UITabBarController:以编程方式切换到不同的视图控制器

Ion*_*rus 8 iphone restore objective-c uitabbarcontroller uinavigationcontroller

在我的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 ..){}块中 - 希望这个技术可以帮助某人.

小智 7

也许这会有所帮助.我所做的是保存所选标签栏项的索引.当应用程序启动时,我检查数字是否大于3,如果是,我将选定的标签栏视图控制器设置为更多导航控制器,然后只需从更多导航控制器推送保存的索引标签栏视图控制器.

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]];
            }
        }
Run Code Online (Sandbox Code Playgroud)


Joh*_*ger 2

我在版本 2 上进行了此操作。

我的代码就在这里并且运行良好。

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Add the tab bar controller's current view as a subview of the window
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[window addSubview:tabBarController.view];
// Settings getLastViewIndex is just, 0,1,2,3 depending on what it was last set.
tabBarController.selectedIndex = [Settings getLastViewIndex];
Run Code Online (Sandbox Code Playgroud)