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.
In addition, you can check for UINavigationController and ask for its topViewController or even check for UITabBarController and ask for selectedViewController . This will get you the view controller that is currently visible to the user.
Pushing a view controller causes its view to be embedded in the navigation interface. If the animated parameter is true , the view is animated into position; otherwise, the view is simply displayed in its final location.
The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property. To access the root view controller, we ask for the first item of the array of view controllers.
Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .
I'm pretty sure that that UITabBarController
did indeed push your current view controller, but that what you are looking for is the view controller of one of this UITabBarController
's tabs, the view controller that was visible in the UITabBarController
at the time this UITabBarController
pushed your view controller on the navigation stack. Possibly this UITabBarController
pushed 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 UITabBarController
at the time your view controller was pushed on the stack, is to use the .selectedViewController
property, so that would result in something like this:
// get the index of the visible VC on the stack
int currentVCIndex = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];
// get a reference to the previous VC
UITabBarController *prevVC = (UITabBarController *)[self.navigationController.viewControllers objectAtIndex:currentVCIndex - 1];
// get the VC shown by the previous VC
EventInformationViewController *prevShownVC = (EventInformationViewController *)prevVC.selectedViewController;
[prevShownVC performSelector:@selector(rateCurrentEvent:)];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With