gpt4 book ai didi

iphone - 以编程方式更改 tabBarController 的选定选项卡

转载 作者:太空狗 更新时间:2023-10-30 03:09:14 24 4
gpt4 key购买 nike

我在 xcode 中创建了一个作为“选项卡栏应用程序”的基本项目,如果 BOOL x 为真,我希望将应用程序加载切换到第二个选项卡。

现在我有:(位于 viewDidLoad 中的 FirstViewController.m)

if(x){
[self.tabBarController setSelectedIndex:1];
}

这会导致页面底部的选定选项卡突出显示第二个选项卡,但 View 仍然是第一个选项卡的 View 。

我该如何将 View 更改为第二个选项卡的 View ?

最佳答案

好吧,我重现了您的问题,并通过将切换逻辑从 -viewDidLoad 移至 -viewDidAppear: 解决了该问题。所以基本上,改变:

- (void)viewDidLoad {
// Other code...
if(x){
[self.tabBarController setSelectedIndex:1];
}
}

到:

- (void)viewDidAppear:(BOOL)animated {
// Other code...
if(x){
[self.tabBarController setSelectedIndex:1];
}
}

现在,至于为什么会这样,我只能猜测,这与事物初始化的顺序有关,没有更多的挖掘。在父选项卡栏 Controller 完成其自身初始化之前,可能会调用您的 View Controller 的 viewDidLoad。等到您的 View 实际出现后,才能确保所有内容都已加载并处于一致状态。

关于iphone - 以编程方式更改 tabBarController 的选定选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4750002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com