以编程方式更改选项卡时,iPhone View不会使用UITabBarController更新 [英] iPhone View doesn't update with UITabBarController when changing tabs programmatically

查看:57
本文介绍了以编程方式更改选项卡时,iPhone View不会使用UITabBarController更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用UITabBarController设置一个新的基于iPhone窗口的项目,然后将所有内容都连接到IB中.使用self.tabBarController.selectedIndex = 1;您应该能够以编程方式切换标签页.但是,它似乎仅在某些情况下有效.如果将其绑定到IBAction或作为图像选择器的结果,则效果很好.但是,如果您调用得太快,它似乎只能部分起作用.在视图中被选中的选项卡仍然是旧视图.

Setup a new iPhone window based project with a UITabBarController and connect everything up in IB. Using self.tabBarController.selectedIndex = 1; you should be able to switch tabs programmatically. But, it only seems to work in certain circumstances. If you tie it to an IBAction or as the result of the image picker it works great. But, if you call it too quickly it seems to only partially work. The tab gets selected at the view is still the old view.

我试图将self.tabBarController.selectedIndex = 1;在viewDidLoad中的switch语句中作为返回到启动时先前选择的选项卡的方法,它将不起作用.然后,我添加了一个NSTimer并尝试在0.01秒后执行此操作,它工作正常,因此显然是定时/订购问题.

I tried to put self.tabBarController.selectedIndex = 1; in a switch statement in the viewDidLoad as a method to return to the previously selected tab on launch and it won't work. I then added an NSTimer and tried to do it .01 seconds later and it works fine, so it's obviously a timing/ordering issue.

关于何时应该明确切换标签的任何建议?

Any suggestions of when I should be clear to switch tabs?

推荐答案

由于您是从基于窗口的项目开始的,因此我假设您正在以编程方式创建标签栏控制器.这里的问题是,一旦您的标签栏控制器被初始化(alloc/init),就会触发viewDidLoad.由于直到此之后才设置选项卡栏控制器的视图控制器属性,因此,当视图控制器计数为0时,将选定的索引设置为1.根据选项卡栏控制器文档,它将吞并错误并设置所选的索引回到0.

Since you started with a window based project, I assume that you're creating your tab bar controller programmatically. The problem here is that viewDidLoad fires as soon as your tab bar controller is initialized (alloc/init). Since your tab bar controller's view controllers property isn't set until after that, you're setting the selected index to 1 when the view controller count is 0. According to the tab bar controller documentation, it will swallow the error and set the selected index back to 0.

延迟起作用的原因是,该延迟可能足够长,以至于您可以在设置所选索引之前设置视图控制器.不过,这很危险,因为如果您的视图控制器由于某种原因加载缓慢,则可能在它们准备就绪之前就设置了选定的索引.

The reason that it works with a delay is that the delay is probably long enough to allow your view controllers to be set before setting the selected index. This is dangerous though, because if your view controllers are slow about loading for any reason, you may be setting the selected index before they're ready.

令人讨厌的是,它与从笔尖加载选项卡栏控制器时的工作方式不同.如果是从笔尖加载的,则在调用viewDidLoad之前,将存在视图控制器.

It's irritating that this it works differently than when the tab bar controller is loaded from a nib. If it's loaded from a nib, the view controllers are present before viewDidLoad is invoked.

无论如何,基本上,您有两个选择,每个选择都涉及子类化UITabBarController:

Anyways, you basically have two options, either of which involves subclassing UITabBarController:

  1. 将视图控制器添加到选项卡栏init/initWithNibName方法中的选项卡栏控制器中.然后,您可以在viewDidLoad中设置选定的索引
  2. 重写setViewControllers方法,并在添加所选索引后对其进行设置.

没有尝试过这两个选项中的任何一个,但由于我遇到了相同的问题,所以我要这么做.

Haven't tried either of these options, but I'm about to because I ran into the same issue.

这篇关于以编程方式更改选项卡时,iPhone View不会使用UITabBarController更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆