I have an app that has a UINavigationController that pushes a UITabBarController into view. This UITabBarController has four tabs, one of which shows a custom UIViewController, an instance of EventInformationViewController. A button in this custom view controller in turn pushes another custom view controller EventRatingAddViewController into view. An action in this view controller should invoke a method in the EventInformationViewController instance. The following code makes the app crash instead:
// get the index of the visible VC on the stack
int myIindex = [self.navigationController.viewControllers indexOfObject:self.navigationController.visibleViewController];
// get a reference to the previous VC
EventInformationViewController *prevVC = (EventInformationViewController *)[self.navigationController.viewControllers objectAtIndex:myIindex - 1];
[prevVC performSelector:@selector(rateCurrentEvent:)];
I thought that the viewControllers property kept an array of all VCs on the navigation stack, so the index the currently visible one minus one should point to the VC that pushed the currently visible VC into view. Rather, it seems to point to my UITabBarController:
-[UITabBarController rateCurrentEvent:]: unrecognized selector sent to instance
What is up with that and more importantly, how do I get a pointer to the VC that pushed the currently visisble VC into view?
EDIT: I ended up creating a delegate protocol for the EventRatingAddViewController and assigning the EventInformationViewController as delegate. This works well – still I am thinking there should be a way to access the pushing VC through the navigation stack.
I’m pretty sure that that
UITabBarControllerdid indeed push your current view controller, but that what you are looking for is the view controller of one of thisUITabBarController‘s tabs, the view controller that was visible in theUITabBarControllerat the time thisUITabBarControllerpushed your view controller on the navigation stack. Possibly thisUITabBarControllerpushed your view controller on the stack, because it was told to do so by the visible tab’s view controller, so it would be something like this:[self.tabBarController.navigationController pushViewController:someViewController];.The way to find out what view controller was shown in the
UITabBarControllerat the time your view controller was pushed on the stack, is to use the.selectedViewControllerproperty, so that would result in something like this: