UITabBarController 奇怪吗?

发布于 2024-11-26 11:20:16 字数 556 浏览 5 评论 0原文

如果我通常加载 UITabBarController 并在视图之间切换,它会调用每个视图控制器的 viewWillAppear 。 但事实并非如此。

我想在触摸另一个选项卡后立即切换到另一个选项卡。

如果我指定要加载的选项卡 - 例如 [self.tabBarController setSelectedIndex:0 ] 在其中一个选项卡(例如选项卡 4)的 viewWillAppear 中...它立即返回到选项卡 0。

但在那之后....它不会调用 viewWillAppear 在任何当我在选项卡之间切换时。 例如,如果我再次转到选项卡 4,它不会返回到选项卡 0。我希望它会通过一个永无止境的循环,因为我希望一旦触摸选项卡 4,选项卡 0 就会加载。

但它仅运行一次!!

为什么 ??


注意:问题已被编辑。

If I normally load up a UITabBarController and switch between views, it calls the viewWillAppear of each view controller.
But it is not so.

I want to switch to another tab as soon as another tab is touched.

If I specify a tab to load up - for example [self.tabBarController setSelectedIndex:0] in the viewWillAppear of one of the tabs (say tab 4)... It goes immediately back to tab 0.

But after that.... it does not call the viewWillAppear on any of the tabs when I switch between them.
For example, if I again go to tab 4, it does not come back to tab 0. I expect it to by a never ending cycle as I expect tab 0 to load up as soon as tab 4 is touch.

But it runs JUST ONCE !!

Why ??


Note: Question has been edited.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零時差 2024-12-03 11:20:16

我想我找到了解决方案。 每次您单击选项卡时它都会起作用,并且它会在两个选项卡上调用 viewWillAppear

您可以在您的 AppDelegate(或 UITabBarController 委托中的其他位置)中执行此操作:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

示例代码:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if([self.tabBarController.viewControllers indexOfObject:viewController] == 1) {
        [self.tabBarController setSelectedIndex:0];
    }
}

I think I found a solution. It works every time you click on your tab and it calls viewWillAppear on both tabs.

You can do this in your AppDelegate (or somwhere else in UITabBarController's delegate):

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

Sample code:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if([self.tabBarController.viewControllers indexOfObject:viewController] == 1) {
        [self.tabBarController setSelectedIndex:0];
    }
}
别忘他 2024-12-03 11:20:16

设置 selectedIndex 不会更改 selectedViewController。您必须更改 selectedViewController 本身。有关更多信息,请参阅文档细节。

setting the selectedIndex won't change the selectedViewController. You will have to change selectedViewController itself. Look at the documentation for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文