2
votes

My app contains a Tab Bar Controller at the root which has to three tabs - each of which are View Controllers embedded in their own Navigation Controllers. Like this: Tab Controller -> Nav Controller -> View Controller. I'm trying to set a property of the second tab's view controller from the first tab like this (as I would when preparing for segue)

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController     

if (tabBarController.selectedIndex == 1) {
        ((CreateViewController *)viewController).myProperty = YES;
}

The method is being called as expected, but as soon as I attempt to set the property the app crashes and I receive the error:

-[UINavigationController setAppearedFromTabBar:]: unrecognized selector sent to instance 0x7fc913d5f510

I have a feeling this has to do with the fact that the View Controller (CreateViewController *) is embedded in a Nav Controller. Do I need to pass the info to the Nav Controller which in turn will pass it to the View Controller? Any help would be appreciated!

1
Clearly you have reference to navigationController instead of CreateViwController. I would try sth like: (CreateViewController *)[(UINavigationController *)viewController topViewController].myProperty = YES;kedzia
very good, it works, thanks! I just had to add extra parentheses so - ((CreateViewController *)[(UINavigationController *)viewController topViewController]).myProperty = YES;Dana
I have posted my comment as an answer, so you may accept it :)kedzia

1 Answers

2
votes

Clearly you have reference to navigationController instead of CreateViwController.

((CreateViewController *)[(UINavigationController *)viewController topViewController]).myProperty = YES;