Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get to the view controller that pushed the visible view controller

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.

like image 961
mvexel Avatar asked Jul 06 '10 12:07

mvexel


People also ask

How do I find my top view controller?

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.

How do I push a view controller?

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.

How do I get navigation controller root view controller?

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.

How do I embed a view controller in navigation controller storyboard?

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 .


1 Answers

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:)];
like image 78
Douwe Maan Avatar answered Nov 04 '22 08:11

Douwe Maan