问题描述
我在xcode中创建了一个基本项目作为Tab Bar Application,如果BOOL x为真,我希望将应用程序加载到第二个选项卡。
I have a basic project created in xcode as a "Tab Bar Application", What I would like is to have the application on load switch to the 2nd tab if BOOL x is true.
现在我有:(位于viewDidLoad中的FirstViewController.m)
Right now I have: (located in FirstViewController.m in viewDidLoad)
if(x){
[self.tabBarController setSelectedIndex:1];
}
这会导致页面底部的选定标签突出显示第二个标签,但视图仍然是第一个标签的视图。
This causes the selected tab at the bottom of the page to highlight the 2nd tab, however the view remains that of the first tab.
我如何将视图更改为第二个标签的视图?
How would I go about changing the view to that of the 2nd tab?
推荐答案
好吧,我重现了你的问题,并通过将切换逻辑从-viewDidLoad移动到-viewDidAppear:来解决它。基本上,改变:
Well, I reproduced your issue, and solved it by moving the switching logic from -viewDidLoad to -viewDidAppear:. So basically, change:
- (void)viewDidLoad {
// Other code...
if(x){
[self.tabBarController setSelectedIndex:1];
}
}
到:
- (void)viewDidAppear:(BOOL)animated {
// Other code...
if(x){
[self.tabBarController setSelectedIndex:1];
}
}
现在,至于为什么会这样,我只能在没有更多挖掘的情况下猜测它与订单事物的初始化有关。在父标签栏控制器完成自己的初始化之前,可能会调用视图控制器的viewDidLoad。暂停直到您的视图实际出现,确保所有内容都已加载并处于一致状态。
Now, as to why this is the case, I can only guess, without more digging, that it has to do with the order things are initialized. It is possible that your view controller's viewDidLoad is being called before the parent tab bar controller has finished its own initialization. Holding off until your view has actually appeared ensures that everything is loaded and in a consistent state.
这篇关于以编程方式更改tabBarController的选定选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!