Question

I'm building an iPad app where i'll use container views.

I use a storyboard, so I added 2 UIViewControllers and 1 UITableViewController.

In one UIViewController i added 2 container views and connected each view to the UITableViewController and to the other UIViewController through an embed segue.

Both container views are visible on app start.

I populated the tableview with an array and i'm trying to pass a string to the other UIViewController through didselectrowatindexpath.

I can see the string is passed to  the viewcontroller by nslogging but the label in the view controller doesn't change.

here is the didselectrowatindexpath in the tableviewcontroller:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailVC *detailVC = [[DetailVC alloc] init];
    NSString *urlString = [siteAddresses objectAtIndex:indexPath.row];
    detailVC.webAddress = urlString;

    [detailVC viewWillAppear:YES];
}

and here is the m file of the recieving view controller:

@synthesize webAddress, webview, webURL;

-(void)viewWillAppear:(BOOL)animated
{

    self.webAddress = webAddress;
    webURL.text = webAddress;
    NSLog(@"%@",webAddress);
    NSLog(@"%@", webURL.text);
    [super viewWillAppear:animated];
}

All outlets and connection are correct, but still the label text doesn't change.

I'm using storyboard to connect the container views to the controllers thru embed segues.

Any help is highly appreciated.

Was it helpful?

Solution

You are creating a new detail view with alloc init. Instead you should reference your existing detail view, e.g. via the parentViewController.

OTHER TIPS

As the other posters have pointed out, you're creating a new instance of your view controller in didSelectRowAtIndexPath, which is wrong.

What you want to do is to write your prepareForSegue so that it checks the segue identifier of each embed segue, and saves a pointer to each view child view controller that you will need to refer to later:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([segue.identifier isEqualToString: @"detailVC"]
    self.detailVC = segue.destinationViewController;
  else if ([segue.identifier isEqualToString: @"childVC2"]
    self.childVC2 = segue.destinationViewController;
  else if ([segue.identifier isEqualToString: @"childTableVC"]
    self.childTableVC = segue.destinationViewController;
}

(Obviously you need to set up your storyboard with meaningful identifiers on your segues and change the code above to match.)

Then your didSelectRowAtIndexPath method might looks like this:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.detailVC updateWebAddressWithString: urlString];
}

Note that you should not be calling a VC's viewWillAppear:animated method directly.

Instead, write a method updateWebAddressWithString that takes a new url string, passes it to the web view, and then triggers an update.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top