1

I have 2 tab bars in a tabbar controller, each with a nav controller. Upon a given action, i would like to execute one command in one, and one in the other one. Example:

// in my current tabbar, pop back to root view
[self.navigationController popToRootViewControllerAnimated:YES];

// go to the first tab bar
self.tabBarController.selectedViewController 
= [self.tabBarController.viewControllers objectAtIndex:0];

When performed individually, the code above works. However, when laid out in the sequence above, only the first command is executed, while the second one is not executed.

The weird is that when i switch the sequence, it works.

What is happening here to cause these 2 different scenarios?

3
  • Make sure that self.tabBarController isn't nil...
    – Simon Lee
    Jun 17, 2011 at 8:13
  • it is not. everything works. what i want to understand better is why the sequence matters here
    – meow
    Jun 17, 2011 at 8:39
  • 1
    Not sure about your hiararchy, but maybe the first command changes the view/controller hiarachy that the second line cannot work.
    – Eiko
    Jun 17, 2011 at 9:47

1 Answer 1

2

As soon as you do,

[self.navigationController popToRootViewControllerAnimated:YES];

You are detaching the view controller from the navigation stack. This will set the navigationController property to nil as it no longer belongs to that stack. It would be inappropriate for the property to contain a valid value.

Same thing applies to the tabBarController property. It would be set as long as the navigation controller was part of the tab bar controller or it was part of that navigation controller. This is set to nil as well at the end of that statement.

Switching the statements will work as it doesn't alter the navigation stack until your task is done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.