开发者

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

开发者 https://www.devze.com 2023-03-13 19:45 出处:网络
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:

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

精彩评论

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