0

I am trying to set a dynamic NSString value each time a table cell is pressed in my app. I am able to pass the variable to my custom UINavigationController with the code below -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSString *page = [menuItems objectAtIndex:indexPath.row];
    [[segue destinationViewController] setGrabPage: page];

}

The problem is that grabPage only gets set in the NavigationController, and not the top view displayed within the navigation controller. How do I pass this value through to the top view?

1 Answers1

0

If your destinationViewController is a UINavigationController, you should cast it into a UINavigationController and get the rootViewController of the navigation controller.

Cast it to the type of this first controller and you're good to go

EDIT: Added sample code

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSString *page = [menuItems objectAtIndex:indexPath.row];

    UINavigationController *nav = (UINavigationController *)[segue destinationViewController];
    MyViewController *vc = (MyViewController *)(nav.viewControllers[0]);
    [vc setGrabPage: page];
}
Diego Freniche
  • 5,225
  • 3
  • 32
  • 45