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

发布于 2024-07-26 13:03:36 字数 769 浏览 2 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

待天淡蓝洁白时 2024-08-02 13:03:36

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

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]];
            }
        }
薄荷→糖丶微凉 2024-08-02 13:03:36

我在版本 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];

I have this working on version 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];
因为看清所以看轻 2024-08-02 13:03:36

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

该属性名义上代表
数组中的索引
viewControllers 属性。 然而,如果
选定的视图控制器是
目前是更多导航
控制器,该属性包含
NSNotFound
。 设置此
属性更改选定的视图
控制器到那个
中的指定索引
viewControllers 数组。 要选择
更多导航控制器本身,您
必须改变的值
selectedViewController 属性
相反。

在 iPhone OS 之前的版本中
3.0版本,该属性反映
所选标签栏项目的索引
仅。
尝试将此值设置为
视图控制器的索引是
在标签栏中不可见,但
而是通过更多导航进行管理
控制器,没有任何效果

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

这个视图控制器是一个
自定义视图当前显示为
标签栏界面。 指定的
视图控制器必须位于
viewControllers 数组。 分配一个新的
查看此属性的控制器
更改当前显示的视图
并在中选择适当的选项卡
标签栏。 改变视图
控制器还更新
相应的 selectedIndex 属性。
该属性的默认值为

在 iPhone OS 3.0 及更高版本中,您可以
使用此属性可以选择任意
查看控制器中的
viewControllers 属性。 这
包括视图控制器
更多导航管理
控制器及其标签栏项目是
在标签栏中不可见。 你可以
还可以用它来选择更多
导航控制器本身,即
可以从
moreNavigationController 属性。
在 iPhone OS 3.0 之前,您可以
仅选择更多导航
控制器和视图的子集
标签栏项目为的控制器
可见的。 尝试设置这个
视图控制器的属性,其
选项卡栏项目不可见 没有
效果。

至于解决方法,我想知道 More 导航控制器的 pushViewController:animated: 方法在这里是否会派上用场 为每个视图提供一个唯一的标签号(您可以将其与幕后适当的 VC 关联)。 保存最后一个活动的 VC 的标签。

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

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

The UITabBarController docs regarding selectedIndex spell it out:

This property nominally represents an
index into the array of the
viewControllers property. However, if
the selected view controller is
currently the More navigation
controller, this property contains the
value NSNotFound
. Setting this
property changes the selected view
controller to the one at the
designated index in the
viewControllers array. To select the
More navigation controller itself, you
must change the value of the
selectedViewController property
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 More navigation
controller, has no effect.

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:

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
viewControllers array. 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
selectedIndex property 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
viewControllers property. This
includes view controllers that are
managed by the More navigation
controller and whose tab bar items are
not visible in the tab bar. You can
also use it to select the More
navigation controller itself, which is
available from the
moreNavigationController property.
Prior to iPhone OS 3.0, you could
select only the More navigation
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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文