Setting a property in a segue with Navigation Controller containing another view

user2529626 picture user2529626 · Nov 4, 2013 · Viewed 20.4k times · Source

I'm getting a crash when trying to set a property in my segue. It's a UIView passing a property to a Navigation Controller that has a TableView as it's root view. It's supposed to go to my TableViewController, but it looks like it is getting intercepted by that NavigationController and throwing an unrecognized selector error.

Segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showItems"]) {
        ShowItemsTableViewController *destinationViewController = [segue destinationViewController];

        [destinationViewController setItems:[self itemsFromCoreData]];
    }
}

Error:

-[UINavigationController setItems:]: unrecognized selector sent to instance 0x10920c840

What's going on here? That NavigationController doesn't have a class associated with it, it's just in the storyboard and comes up modally. It works if I set the segue in the storyboard to go directly to the view, not the NavigationController, but I really need the nav there. How do I get around this?

Answer

Kamaros picture Kamaros · Nov 4, 2013

Since the destination view controller is actually the navigation controller, try accessing the root view like so:

UINavigationController *navController = [segue destinationViewController];
ShowItemsTableViewController *SITViewController = (ShowItemsTableViewController *)([navController viewControllers][0]);
[SITViewController setItems:[self itemsFromCoreData]];