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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1095471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:52:32  来源:igfitidea点击:

UITabBarController: switch to a different view controller programmatically

iphoneobjective-cuinavigationcontrolleruitabbarcontrollerrestore

提问by Ionic Walrus

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 应用程序中,为了恢复以前查看过的选项卡,在启动时我设置了 setSelectedIndex:(也尝试过 setSelectedViewController: 按照文档但无济于事)

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 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

Im wondering if its possible to switch to a view controller under iPhone OS 2.x ? Any help is appreciated.

我想知道是否可以在 iPhone OS 2.x 下切换到视图控制器?任何帮助表示赞赏。

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 会引发错误(对于 iPhone OS 2.x) - 所以我将它包装在 @try{..} @catch(id ..){ } 块中 - 希望这项技术可以帮助某人.

回答by Ionic Walrus

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.

也许这会有所帮助。我所做的是保存所选标签栏项目的索引。当应用程序启动时,我检查数字是否大于 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]];
            }
        }

回答by John Ballinger

I have this working on version 2.

我在版本 2 上有这个工作。

My code sits here and is working lovely.

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

- (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];

回答by Joe D'Andrea

The UITabBarController docs regarding selectedIndexspell it out:

有关selectedIndex的 UITabBarController 文档说明了这一点:

This property nominally represents an index into the array of the viewControllersproperty. However, if the selected view controller is currently the Morenavigation controller, this property contains the value NSNotFound. Setting this property changes the selected view controller to the one at the designated index in the viewControllersarray. To select the Morenavigation controller itself, you must change the value of the selectedViewControllerproperty instead.

In versions of iPhone OS prior to version 3.0, this property reflects the index of the selected tab bar item only.Attempting to set this value to an index of a view controller that is not visible in the tab bar, but is instead managed by the Morenavigation controller, has no effect.

此属性名义上表示属性数组的索引 viewControllers。但是,如果选定的视图控制器当前是更多导航控制器,则此属性包含值NSNotFound。设置此属性会将选定的视图控制器更改为viewControllers数组中指定索引处的视图控制器 。要选择 更多导航控制器本身,您必须改为更改该selectedViewController属性的值 。

在 3.0 版之前的 iPhone OS 版本中,此属性仅反映所选标签栏项目的索引。尝试将此值设置为在选项卡栏中不可见但由“更多”导航控制器管理的视图控制器的索引无效

If I understand correctly, you need to "change the value of the selectedViewControllerproperty" instead, but you'll only get as far as selecting the Morenav controller, not a VC within it. From the same docs regarding selectedViewController:

如果我理解正确,您需要“更改selectedViewController属性的值”,但您只会选择更多导航控制器,而不是其中的 VC。来自关于selectedViewController的相同文档:

This view controller is the one whose custom view is currently displayed by the tab bar interface. The specified view controller must be in the viewControllersarray. Assigning a new view controller to this property changes the currently displayed view and also selects an appropriate tab in the tab bar. Changing the view controller also updates the selectedIndexproperty accordingly. The default value of this property is nil.

In iPhone OS 3.0 and later, you can use this property to select any of the view controllers in the viewControllersproperty. This includes view controllers that are managed by the Morenavigation controller and whose tab bar items are not visible in the tab bar. You can also use it to select the Morenavigation controller itself, which is available from the moreNavigationControllerproperty. Prior to iPhone OS 3.0, you could select only the Morenavigation controller and the subset of view controllers whose tab bar item was visible. Attempting to set this property to a view controller whose tab bar item was not visible had no effect.

此视图控制器是当前由选项卡栏界面显示的自定义视图。指定的视图控制器必须在 viewControllers数组中。为这个属性分配一个新的视图控制器会改变当前显示的视图,并且还会在选项卡栏中选择一个合适的选项卡。更改视图控制器也会相应地更新 selectedIndex属性。此属性的默认值为 nil

在 iPhone OS 3.0 及更高版本中,您可以使用此属性来选择viewControllers属性中的任何视图控制器 。这包括由更多导航控制器管理的视图控制器,其标签栏项目在标签栏中不可见。您还可以使用它来选择More导航控制器本身,它可以从 moreNavigationController属性获得。 在 iPhone OS 3.0 之前,您只能选择更多导航控制器和选项卡栏项可见的视图控制器子集。尝试将此属性设置为选项卡栏项目不可见的视图控制器无效。

As for a workaround, I wonder if the Morenav 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.

至于解决方法,我想知道更多导航控制器的pushViewController:animated:方法在这里会派上用场吗?为每个视图指定一个唯一的标签编号(您可以将其与幕后适当的 VC 相关联)。保存上次处于活动状态的 VC 的标签。

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 Morenav controller. Locate the VC, push it onto the Morenav controller's stack, then select the Morenav controller outright.

在启动时,在标签栏控制器中选择适当的视图。如果视图的标签未与前四个选项卡项的 VC 相关联,则它必须位于更多导航控制器内。找到 VC,将其推送到更多导航控制器的堆栈上,然后直接选择更多导航控制器。

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 afterthe Morenav controller is setup, not before.

我没有试过这个,但它可能值得一试!唯一潜在的问题(它可能是一个大问题)是您必须设置更多导航控制器之后而不是之前推动该 VC 。