user2529626
user2529626

Reputation:

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

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?

Upvotes: 34

Views: 20787

Answers (4)

Guto Araujo
Guto Araujo

Reputation: 3854

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];
    }
}

Upvotes: 8

Stiño
Stiño

Reputation: 2853

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
    }
}

Upvotes: 8

Kamaros
Kamaros

Reputation: 4566

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]];

Upvotes: 75

David Hoerl
David Hoerl

Reputation: 41642

Look at the view controller class in the prepare for segue - it's the nav controller. Cast it to that class, then you can get to the desired view by asking it for its top view controller.

Upvotes: 0

Related Questions