开发者

Objective C: Executing a command in tabbar immediately before switching away

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 开发者_StackOverflow社区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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜