it's now a couple of days I've been struggling with UISplitViewControllers, here's my problem: I have a master view which is declared as follows
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
#class DetailViewController;
#interface MasterViewController : UITableViewController {
NSMutableArray *title, *subTitle;
unsigned int quantity;
}
#property (strong, nonatomic) DetailViewController *detailViewController;
#end
In the master's .m file everything works correctly (I'm able to populate the table from a sqlite db and show the contents in rows). When it comes to select one row and populate the detailed view on the right side nothing happens. Here is the row selection:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyObject itemFromDB;
/*
... DB stuff here ...
*/
self.detailViewController.detailItem = itemFromDB;
}
Here's now the DetailViewController implementation:
#interface DetailViewController : UIViewController <UISplitViewControllerDelegate>
#property (strong, nonatomic) MyObject *detailItem;
#property (weak, nonatomic) IBOutlet UILabel *name, *phone;
#end
And the detail's .m:
#import "DetailViewController.h"
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
- (void)setDetailItem:(MyObject *)newItem
{
if (_detailItem != newItem) {
_detailItem = newItem;
[self configureView];
}
}
- (void)configureView
{
if (self.detailItem) {
self.name.text = [self.detailItem name];
self.phone.text = [self.detailItem phone];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
In simple words: the detail isn't noticing any changes after the selection of a row. Any help?
You never defined self.detailViewController. You should do this:
self.detailViewController = self.splitViewController.viewControllers[1];
Related
I'm having two view controller A and B. when click button on view controllerA it will navigate to view controllerB and their is a tableview with textlabel,when i click on tableviewcell i want to navigate back into viewcontrollerA and want to pass that textlabel value back to viewcontrollerA
if you are using push seque is the worst way, reason you increase the memory in your stack trace.
you can do this in multiple ways, search once in google you get multiple answer related to this
UnWind Segue
Protocols & delegates
local database method
using Singleton class.
Using bean object method.
using Plist
finally go for NSUserDefault
I created a sample using Protocols and Delegates, as follows:
ViewController.m
#import "ViewController.h"
#import "ColorsTableViewController.h"
#interface ViewController () <ColorsDelegate>
#property (weak, nonatomic) IBOutlet UILabel *textDataLabel;
- (IBAction)goButtonTapped:(id)sender;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)sendColor:(NSString *)colorName{
self.textDataLabel.text = colorName;
}
- (IBAction)goButtonTapped:(id)sender {
ColorsTableViewController *colorsTableViewController=(ColorsTableViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"colorTableIdentifier"];
colorsTableViewController.delegate = self;
[self.navigationController pushViewController:colorsTableViewController animated:YES];
}
#end
ColorsTableViewController.h
#import <UIKit/UIKit.h>
#protocol ColorsDelegate <NSObject>
#optional
- (void)sendColor:(NSString *)colorName;
#end
#interface ColorsTableViewController : UIViewController
#property (nonatomic, assign) id<ColorsDelegate> delegate;
#end
ColorsTableViewController.m
#import "ColorsTableViewController.h"
#interface ColorsTableViewController () <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *colorsTableView;
#property (strong, nonatomic) NSArray *colorsArray;
#end
#implementation ColorsTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.colorsArray = #[#"Red",#"Green",#"Blue",#"Brown",#"Yellow"];
[self.colorsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"colorCell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.colorsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *myCell = [self.colorsTableView dequeueReusableCellWithIdentifier:#"colorCell" forIndexPath:indexPath];
UILabel *myTextLabel = [myCell.contentView viewWithTag:100];
if(!myTextLabel)
{
myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, CGRectGetWidth(myCell.frame), CGRectGetHeight(myCell.frame))];
myTextLabel.tag = 100;
}
myTextLabel.text = self.colorsArray[indexPath.row];
[myCell.contentView addSubview:myTextLabel];
return myCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *myCell = (UITableViewCell *)[self.colorsTableView cellForRowAtIndexPath:indexPath];
UILabel *myTextLabel = [myCell.contentView viewWithTag:100];
if(myTextLabel)
{
[_delegate sendColor:myTextLabel.text];
}
}
Screenshot:
Here is my GitHub Link, please download and check:
https://github.com/k-sathireddy/sendDataBack
What i want to do is multiply the latest NSMutableArray array entry (entered using a UITextField) by 3 when Moderate intensity is selected using the Plain Segmented Control and 6 when vigorous is selected and then display the total value of all entries in the array after the multiplications have occurred. E.g. If there User selects Moderate using the Plain Segmented Control and enters 120 in the UITextField, I need a value of 360 to be displayed and for that value to increment as more entries are made.
So far I'm storing the array values in a table like below which works fine.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"SecondViewControllerSegue"]) {
SecondViewController *secondViewController
= [segue destinationViewController];
//secondViewController.infoRequest = self.nameField.text;
NSString* style = (styleSeg.selectedSegmentIndex == 0) ? #"Moderate intensity for" : #"Vigourous intensity for";
[activities addObject:[[NSString alloc]initWithFormat:#"Your activity: %#", self.activityField.text]];
secondViewController.activities = activities;
[activities addObject:[[NSString alloc]initWithFormat:#"%#: %# minutes", style, self.nameField.text]];
secondViewController.activities = activities;
}
}
I just can't seem to multiply and output the values. I've been playing around with something like
if(styleseg.selectedSegmentIndex == 0){
3x(what the user entered in duration)
}
if(styleseg.selectedSegmentIndex == 1){
6x(what the user entered in duration)
}
And a loop attempting to add up the total values in the array which is just outputting 0.
int result = 0;
for(int i=0;i<[activities count];i++)
result += [[activities objectAtIndex:i] intValue];
NSLog(#"result = %d", result);
I'm just having trouble blending the two together to do what I want. Any help is greatly appreciated.
NEW EDIT
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIView *nameView;
IBOutlet UITextField *nameField;
IBOutlet UITextField *activityField;
IBOutlet UISegmentedControl *styleSeg;
}
#property UIView *nameView;
#property UITextField *nameField;
#property UITextField *activityField;
#property (strong,nonatomic) NSMutableArray *activities;
- (IBAction)submitButtonTapped:(id)sender;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
#import "MyActivity.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize nameView;
#synthesize nameField;
#synthesize activityField;
#synthesize activities;
- (void)viewDidLoad
{
[super viewDidLoad];
activities = [[NSMutableArray alloc] init];
//activityName = [[NSMutableArray alloc] init];
}
-(void)viewWillAppear:(BOOL)animated
{
self.nameField.text = #"";
self.activityField.text = #"";
styleSeg.selectedSegmentIndex = 0;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)submitButtonTapped:(id)sender {
NSLog(#"The submit button was clicked.");
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"SecondViewControllerSegue"]) {
SecondViewController *secondViewController
= [segue destinationViewController];
secondViewController.infoRequest = self.nameField.text;
/* This was my initial code as in the intial question
NSString* style = (styleSeg.selectedSegmentIndex == 0) ? #"Moderate intensity for" : #"Vigourous intensity for";
[activities addObject:[[NSString alloc]initWithFormat:#"Your activity: %#", self.activityField.text]];
secondViewController.activities = activities;
[activities addObject:[[NSString alloc]initWithFormat:#"%#: %# minutes", style, self.nameField.text]];
secondViewController.activities = activities;
*/
// New code
MyActivity *activity=[[MyActivity alloc]init];
activity.description=self.activityField.text;
activity.duration=[self.nameField.text intValue];
activity.intensity=(styleSeg.selectedSegmentIndex == 0) ? 3:6;
[self.activities addObject:activity];
secondViewController.activities = activities;
}
}
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
IBOutlet UIView *secondView;
IBOutlet UILabel *nameLabel;
}
#property IBOutlet UITableView *activityTableView;
#property NSMutableArray* activities;
//#property NSMutableArray* activityName;
#property UIView *secondView;
#property UILabel *nameLabel;
#property (weak, nonatomic) IBOutlet UILabel *nameLabel2;
#property id infoRequest;
-(IBAction)goBack:(id)sender;
#end
SecondViewController.m
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize secondView;
#synthesize nameLabel;
#synthesize nameLabel2;
#synthesize activities;
#synthesize infoRequest;
#synthesize activityTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.nameLabel.text = [self.infoRequest description];
self.nameLabel2.text = [self.infoRequest description];
// activities = [[NSArray alloc] init];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)goBack:(id)sender
{
UINavigationController* parent = (UINavigationController*)[self parentViewController];
[parent popViewControllerAnimated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Number of rows is the number of time zones in the region for the specified section.
return [activities count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellReuseIdentifier = #"CellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
}
NSString* s = [activities objectAtIndex:indexPath.row];
cell.textLabel.text = s;
return cell;
}
Your array seems to hold arbitrary strings, not integers represented as strings (the intValue of "10" is 10, but the intValue of "Moderate intensity for: 10 minutes" is 0). Also you are adding multiple elements for each instance of an activity - the activity description and the activity duration.
I would create another object class to encapsulate the activity -
MyActivity.h
#interface MyActivity : NSObject
#property (copy,nonatomic) NSString *description;
#property int duration;
#property int intensity;
#end
Create a UIButton and set it's touch up inside event to doAddToArray. This will add an entry to the array for each new activate. Allocate a new MyActivity and set the appropriate properties before adding it to the array -
In ViewController.m
-(IBAction)doAddToArray:(id)sender {
MyActivity *activity=[[MyActivity alloc]init];
activity.description=self.activityField.text;
activity.duration=[self.nameField.text intValue];
activity.intensity=(styleSeg.selectedSegmentIndex == 0) ? 3:6;
[self.activities addObject:activity];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"SecondViewControllerSegue"]) {
SecondViewController *secondViewController
= [segue destinationViewController];
secondViewController.activities = activities;
}
Then in SecondViewController.h
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
IBOutlet UIView *secondView;
IBOutlet UILabel *nameLabel;
}
#property IBOutlet UITableView *activityTableView;
#property UIView *secondView;
#property UILabel *nameLabel;
#property (weak, nonatomic) IBOutlet UILabel *nameLabel2;
#property (weak, nonatomic) NSArray *activities; // This is used to provide content to the UITableView
#property id infoRequest;
-(IBAction)goBack:(id)sender;
#end
For brevity I won't include the full SecondViewController.m, but you have a number of places where you use activities that should be self.activities
Then to total the array (wherever you need to)-
int total=0;
for (MyActivity *activity in self.activities)
{
total+=activity.duration*activity.intensity;
}
I'm trying to implement an iCarousel that will pass information on to two other view controllers when an image is chosen. While the iCarousel loads perfectly and transitions to the next VC, the information is not displayed on the new VC.
The approach I chose was to create an NSObject file. I can't simply pass the info from VC to VC since I have several VC's that need the information and I'd prefer not to create a singleton or use AppDelegate if possible.
FYI: I do have a tap gesture recognizer added on top of the UIView that acts as the segue to the next VC if that makes any difference.
I've tried every possible tutorial out there and can't seem to figure out my problem. I just need to display a text label and a picture, which should really be pretty easy. Can someone take a quick glance at my code to see what I'm doing wrong?
My NSObject File:
#import <Foundation/Foundation.h>
#interface Stop : NSObject
#property (nonatomic, strong) NSString *title;
#property (nonatomic, strong) NSString *image;
#end
First ViewController.h (with iCarousel on it):
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "iCarousel.h"
#import "DirectionsViewController.h"
#import "Stop.h"
#interface StopsMenuViewController : UIViewController <iCarouselDataSource, iCarouselDelegate>
#property (strong, nonatomic) IBOutlet iCarousel *carousel;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
//Title
#property (nonatomic, strong) NSArray *stopTitles;
#property (nonatomic, strong) NSString *stopChosen;
//Image
#property (nonatomic, strong) NSArray *stopImages;
#property (nonatomic, strong) NSString *imageChosen;
#end
First ViewController.m:
#import "StopsMenuViewController.h"
#interface StopsMenuViewController () {
NSMutableArray *allInfo; }
#end
#implementation StopsMenuViewController
#synthesize titleLabel, carousel, stopImages, stopTitles, stopChosen, imageChosen;
- (void)awakeFromNib {
NSString *myPlist = [[NSBundle mainBundle] pathForResource:#"Chinatown" ofType:#"plist"];
NSDictionary *rootDictionary = [[NSDictionary alloc] initWithContentsOfFile:myPlist];
self.stopImages = [rootDictionary objectForKey:#"StopImages"];
self.stopTitles = [rootDictionary objectForKey:#"StopTitles"];
}
- (void)carouselDidScroll:(iCarousel *)carousel {
[titleLabel setText:[NSString stringWithFormat:#"%#", [self.stopTitles
objectAtIndex:self.carousel.currentItemIndex]]];
}
- (void)dealloc {
self.carousel.delegate = nil;
self.carousel.dataSource = nil;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"toDirections"])
{
DirectionsViewController *dvc = [segue destinationViewController];
int itemId = [self.carousel currentItemIndex];
NSIndexPath *path = [NSIndexPath indexPathForRow:itemId inSection:0];
Stop *current = [allInfo objectAtIndex:path.row];
[dvc setPassInfo:current];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.carousel.type = iCarouselTypeCoverFlow2;
allInfo = [[NSMutableArray alloc] init];
Stop *info = [[Stop alloc] init];
stopChosen = [NSString stringWithFormat:#"%#", [self.stopTitles objectAtIndex:self.carousel.currentItemIndex]];
[info setTitle:stopChosen];
[allInfo addObject:info];
info = [[Stop alloc] init];
self.imageChosen = [NSString stringWithFormat:#"%#", [self.stopImages
objectAtIndex:self.carousel.currentItemIndex]];
[info setTitle:self.imageChosen];
[allInfo addObject:info];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.carousel = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return [self.stopImages count];
}
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel {
return 4;
}
- (UIView *)carousel:(iCarousel *)_carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
if (view == nil)
{
view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.stopImages objectAtIndex:index]]];
}
return view;
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
DirectionsViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"dvc"];
[self.navigationController pushViewController:dvc animated:YES];
}
#end
Second ViewController.h:
#import <UIKit/UIKit.h>
#import "Stop.h"
#interface DirectionsViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UIImageView *imageBox;
#property (nonatomic, strong) Stop *PassInfo;
#property (nonatomic, strong) NSString *stopTitle;
#property (nonatomic, strong) NSString *myStopTitle;
#end
Second ViewController.m:
#import "DirectionsViewController.h"
#interface DirectionsViewController ()
#end
#implementation DirectionsViewController
#synthesize PassInfo;
- (void)viewDidLoad {
[super viewDidLoad];
[self.titleLabel setText:[PassInfo title]];
UIImage *image = [UIImage imageNamed:[PassInfo image]];
[self.imageBox setImage:image];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
Instead of
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
DirectionsViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"dvc"];
[self.navigationController pushViewController:dvc animated:YES];
}
Use
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
[self performSegueWithIdentifier:#"toDirections" sender:self];
}
In your code, you're instantiating the second view controller and presenting it, which is not the same as performing a segue. Therefore the method - prepareForSegue:sender: will not be invoked.
I have an app, where I making some calculation, and need to transfer result of this calculation to ViewController3 and show the result there. Now I use label in same ViewController2 where I have calculation. Thank you for your help.
ViewController2.h
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController<UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UILabel *gasPrice;
#property (weak, nonatomic) IBOutlet UILabel *gasCarMileage;
#property (weak, nonatomic) IBOutlet UITextField *perGalon;
#property (weak, nonatomic) IBOutlet UITextField *miles;
#property(nonatomic, copy, readonly) NSString *result;
- (IBAction)getIt:(id)sender;
#end
ViewController2.m
#import "ViewController2.h"
#import "ViewController3.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//Start calculation
- (IBAction)getIt:(id)sender; {
float perGalon = ([_perGalon.text floatValue]);
float miles = ([_miles.text floatValue]);
float mileCost = perGalon / miles;
[self performSegueWithIdentifier:#"viewController3" sender: nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"viewController3"]) {
ViewController3 *viewController3 = [segue destinationViewController];
viewController3.result = [[NSString alloc] initWithFormat: #"Every mile you drive
will cost you $ %f", mileCost];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.perGalon.delegate = self;
self.miles.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
#end
ViewController3.h
#import <UIKit/UIKit.h>
#interface ViewController3 : UIViewController
#property(nonatomic, copy) NSString *result;
#end
ViewController3.m
#import "ViewController3.h"
#import "ViewController2.h"
#interface ViewController3 ()
#end
#interface ViewController2 ()
#property(nonatomic, copy, readwrite) NSString *result;
#end
#implementation ViewController3
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You can define a string property in View controller 3 which you will set when you push view controller 3 from view controller 2.
ViewController3 interface declaration
#interface ViewController2 ()
#property(nonatomic, copy) NSString *result;
#end
In ViewController 2, you will implement this line at the last of your getIt() method.
[self performSegueWithIdentifier:#"viewController3" sender: nil]
And implement another method called prepareForSegue as follows
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"viewController3"]) {
ViewController3 *viewController3 = [segue destinationViewController];
viewController3.result = "YOUR CALCULATION RESULT HERE"
}
}
Bear in mind, you have to set identifier for view controller 3, go to storyboard and select the view for view controller 3 and On identify inspector, specified Storyboard ID as "viewController3".
Use a protocol,
In ViewController2.h
#protocol ViewController2Delegate;
#property (strong, nonatomic) id<ViewController2Delegate> delegate;
#protocol BexAPIClientDelegate <NSObject>
- (void)ViewController2:(ViewController2 *)vc didCalculateCost:(CGFloat)cost;
#end
Then at the end of the getIt() call the delegate
[self.delegate viewController2:self didCalculateCost:gasCostPerMile];
You need to set ViewController3 to be ViewController2's delegate when you load it. Also you will need to setup ViewController3 to conform to the protocol.
Using the protocol is worth the effort as it will make your code clear and easy to manage.
Retain a string in VC3. While initializing the VC3 object, set the value from VC2. Update the label in VC3(in viewDidLoad method)
I am trying to call a detail screen from a UITableView list - but the delegate is not being called in the receiving view - I'll post all the code:
list header file:
#import <UIKit/UIKit.h>
#import "tank.h"
#class iTanksV2ListViewController;
#protocol iTanksV2ListViewControllerDelegate
- (void) iTanksListViewController:(iTanksV2ListViewController *) sender choseTank:(tank *)tank;
#end
#interface iTanksV2ListViewController : UITableViewController
#property (nonatomic, strong) NSArray *tanks;
#property (weak, nonatomic) IBOutlet UITableView *tankTableView;
#property (weak, nonatomic) id <iTanksV2ListViewControllerDelegate> delegate;
#end
and the m file:
#import "iTanksV2ListViewController.h"
#import "tank.h"
#import "tankDetailViewController.h"
#interface iTanksV2ListViewController ()
#end
#implementation iTanksV2ListViewController
#synthesize tanks = _tanks;
#synthesize tankTableView = _tankTableView;
#synthesize delegate = _delegate;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[self setTankTableView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;//keep this section in case we do need to add sections in the future.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.tanks count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Tank List Table Cell";
UITableViewCell *cell = [self.tankTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
}
tank *thisTank = [self.tanks objectAtIndex:indexPath.row];
cell.textLabel.text = thisTank.tankNumber;
return cell;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"Show Tank Details"])
{
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
tank *thisTank = [self.tanks objectAtIndex:indexPath.row];
[self.delegate iTanksListViewController:self choseTank:thisTank];
}
#end
and the header for the receiving file:
#import <UIKit/UIKit.h>
#import "tankGauge.h"
#import "tank.h"
#interface tankDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *tankNumberLabel;
#property (weak, nonatomic) IBOutlet UILabel *tankProductLabel;
#property (weak, nonatomic) IBOutlet UILabel *tankAvailableProductLabel;
#property (weak, nonatomic) IBOutlet UILabel *tankMaxVolumeLabel;
#property (weak, nonatomic) IBOutlet tankGauge *tankVolumeGauge;
#property (weak, nonatomic) tank* tankToShow;
#end
...and the m file:
#import "tankDetailViewController.h"
#import "iTanksV2ListViewController.h"
#interface tankDetailViewController () <iTanksV2ListViewControllerDelegate>
#end
#implementation tankDetailViewController
#synthesize tankNumberLabel = _tankNumberLabel;
#synthesize tankProductLabel = _tankProductLabel;
#synthesize tankAvailableProductLabel = _tankAvailableProductLabel;
#synthesize tankMaxVolumeLabel = _tankMaxVolumeLabel;
#synthesize tankVolumeGauge = _tankVolumeGauge;
#synthesize tankToShow = _tankToShow;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)iTanksListViewController:(iTanksV2ListViewController *)sender choseTank:(id)tank
{
self.tankToShow = tank;
self.tankNumberLabel.text = self.tankToShow.tankNumber;
}
- (void)viewDidUnload
{
[self setTankNumberLabel:nil];
[self setTankProductLabel:nil];
[self setTankAvailableProductLabel:nil];
[self setTankMaxVolumeLabel:nil];
[self setTankVolumeGauge:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
tankTableView is an IBOutlet, so you just need to connect your tableView's delegate and data-source to your File's Owner in your xib as shown below:
Have you set that viewcontroller as the delegate for that tableview?