UITabBarController:以编程方式切换到不同的视图控制器 [英] UITabBarController: switch to a different view controller programmatically
问题描述
在我的iPhone应用程序中,要恢复以前查看过的标签,在启动时我设置了setSelectedIndex :(也尝试过setSelectedViewController:根据文档但无效)
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)
这适用于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
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
我想知道是否可以在iPhone OS 2.x下切换到视图控制器?任何帮助都表示赞赏。
Im wondering if its possible to switch to a view controller under iPhone OS 2.x ? Any help is appreciated.
我的模拟器设置索引大于3的Btw会引发错误(对于iPhone OS 2.x) - 所以我把它包装在@try中{..} @catch(id ..){}阻止 - 希望这种技术可以帮助某人。
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.
推荐答案
也许这会有所帮助。我所做的是保存所选标签栏项的索引。当应用程序启动时,我检查数字是否大于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.
if ([[WSFUserDefaults sharedInstance] savedTabBarLocation] > 0) {
if ([[WSFUserDefaults sharedInstance] savedTabBarLocation] > 3) {
UIViewController *selectViewController = [tabBarController.viewControllers objectAtIndex:[[WSFUserDefaults sharedInstance] savedTabBarLocation]];
[tabBarController setSelectedViewController:tabBarController.moreNavigationController];
[tabBarController.moreNavigationController popToRootViewControllerAnimated:NO];//make sure we're at the top level More
[tabBarController.moreNavigationController pushViewController:selectViewController animated:NO];
}
else {
[tabBarController setSelectedIndex:[[WSFUserDefaults sharedInstance] savedTabBarLocation]];
}
}
这篇关于UITabBarController:以编程方式切换到不同的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!