Setting a property in a segue with Navigation Controller containing another view
Solution 1
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]];
Solution 2
Get the topViewController from the UINavigationController:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showItems"]) {
UINavigationController *navigationController = segue.destinationViewController;
ShowItemsTableViewController *showItemsTVC = (ShowItemsTableViewController * )navigationController.topViewController;
showItemsTVC.items = [self itemsFromCoreData];
}
}
Solution 3
For Swift:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueShowNavigation" {
var DestViewController = segue.destinationViewController as! UINavigationController
let targetController = DestViewController.topViewController as! ReceiveViewController
}
}
Admin
Updated on March 13, 2020Comments
-
Admin about 4 years
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?