【问题标题】:Changing tabBarController.selectedViewController only works once?更改 tabBarController.selectedViewController 只能工作一次?
【发布时间】:2011-08-04 03:45:55
【问题描述】:

我的 iPhone 应用程序使用 UITabBarController。在第一个选项卡上,有一个场景,用户可以在不更改选项卡的情况下深入两个 UIViewController。所以屏幕流程是这样的:

  1. 应用加载时默认打开第一个选项卡。
  2. 用户按下 UIButton,将他们带到同一选项卡中的第二个 UIViewController。
  3. 从第二个 UIViewController,用户可以按下另一个 UIButton 将他们带到第三个 UIViewController,仍然在同一个选项卡中

这在使用以下两行从第一个 UIViewController 切换到第二个时有效:

    2ndViewController = [[SecondViewController alloc] init];
    self.tabBarController.selectedViewController = 2ndViewController;

但是,当我尝试从第二个 UIViewController 到第三个执行相同操作时,它不起作用。按钮不做任何事情,我停留在第二个 UIViewController 上。

谁能解释这是为什么?有我可以使用的解决方法吗?如果需要,我很乐意提供更多代码。

提前致谢。

【问题讨论】:

  • 你试过使用'selectedIndex'属性吗?
  • 如果我没记错的话,'selectedIndex' 属性会改变当前选择的选项卡。 tabBarController.selectedIndex = 0 将您放在第一个选项卡上,tabBarController.selectedIndex = 1 将您放在第二个选项卡上,等等。我想在更改 UIViewControllers 时保持在同一个选项卡上。

标签: iphone objective-c uiviewcontroller uitabbarcontroller


【解决方案1】:

这不是该属性的用途。您很可能遇到了文档中提到的“未定义行为”。

做你正在做的事情的最佳方法是在选项卡中放置一个UINavigationController,然后用它推动每个控制器。如果您不想显示导航栏,可以使用navigationBarHidden 将其隐藏。

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 2015-09-01
    • 1970-01-01
    相关资源
    最近更新 更多