UITabBarController:以编程方式切换到不同的视图控制器
在我的 iPhone 应用程序中,为了恢复以前查看的选项卡,在启动时我设置了 setSelectedIndex: (也尝试了 setSelectedViewController: 根据文档,但无济于事)
这适用于 iPhone OS 3.0 - 但在 OS 2.x 上,所选索引大于 3 (前 4 个选项卡)不会切换到所需的视图。 Apple 在此记录了这一点: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITabBarController_Class/Reference/Reference.html#//apple_ref/occ/instp/UITabBarController/selectedViewController
我想知道是否可以在 iPhone OS 2.x 下切换到视图控制器? 任何帮助表示赞赏。
顺便说一句,在我的模拟器上设置索引大于 3 会引发错误(对于 iPhone OS 2.x) - 所以我将其包装在 @try{..} @catch(id ..){ } 块中 - 希望这项技术可以帮助某人。
In my iPhone app, to restore previously viewed tab, on launch I set the setSelectedIndex: (also tried setSelectedViewController: as per docs but to no avail)
This works on iPhone OS 3.0 - however on OS 2.x the selected index greater than 3 (the first 4 tabs) doesn't switch to the required view. This is documented by Apple here: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITabBarController_Class/Reference/Reference.html#//apple_ref/occ/instp/UITabBarController/selectedViewController
Im wondering if its possible to switch to a view controller under iPhone OS 2.x ? Any help is appreciated.
Btw on my simulator setting index greater than 3 throws an error (for iPhone OS 2.x) - so I have wrapped this in a @try{..} @catch(id ..){ } block - hope this technique helps someone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许这会有所帮助。 我所做的是保存所选选项卡栏项目的索引。 当应用程序启动时,我检查数字是否大于 3,如果是,我将选定的选项卡栏视图控制器设置为更多导航控制器,然后只需从更多导航控制器推送保存的索引选项卡栏视图控制器。
Maybe this will help. What I did was save the index of the tab bar item that was selected. When the app launches I check to see if the number is greater than 3, if it is I set the selected tab bar view controller to be the more navigation controller and then just push the saved index tab bar view controller from the more navigation controller.
我在版本 2 上进行了此操作。
我的代码位于此处并且运行良好。
I have this working on version 2.
My code sits here and is working lovely.
关于 selectedIndex 的 UITabBarController 文档说明了这一点:
如果我理解正确,您需要“更改 selectedViewController 属性的值”,但您只能选择 更多 导航控制器,而不是其中有VC。 来自有关 selectedViewController 的相同文档:
至于解决方法,我想知道 More 导航控制器的
pushViewController:animated:
方法在这里是否会派上用场 为每个视图提供一个唯一的标签号(您可以将其与幕后适当的 VC 关联)。 保存最后一个活动的 VC 的标签。在启动时,在选项卡栏控制器中选择适当的视图。 如果视图的标签未与前四个选项卡项的 VC 关联,则它必须位于更多导航控制器内。 找到 VC,将其推入更多导航控制器的堆栈中,然后直接选择更多导航控制器。
我还没有尝试过这个,但可能值得一试! 唯一潜在的问题(这可能是一个大问题)是,您必须在设置更多导航控制器之后(而不是之前)推送该 VC。
The UITabBarController docs regarding selectedIndex spell it out:
If I understand correctly, you need to "change the value of the selectedViewController property" instead, but you'll only get as far as selecting the More nav controller, not a VC within it. From the same docs regarding selectedViewController:
As for a workaround, I wonder if the More nav controller's
pushViewController:animated:
method would come in handy here? Give each view a unique tag number (which you could associate with an appropriate VC behind the scenes). Save the tag for whichever VC was last active.At startup-time, select the appropriate view in the tab bar controller. If the view's tag isn't associated with the VCs for the first four tab items, it must be within the More nav controller. Locate the VC, push it onto the More nav controller's stack, then select the More nav controller outright.
I haven't tried this, but it might be worth an experiment! The only potential gotcha (and it could be a biggie) is that you will have to push that VC after the More nav controller is setup, not before.