在 Xcode 中以编程方式按下 UITabBar 按钮

人气:333 发布:2022-10-16 标签: iphone objective-c cocoa-touch uinavigationcontroller uitabbar

问题描述

抱歉新手问题.我的主窗口视图中有一个 UITabBar 以及每个选项卡的 UINavigationControllers 数组.该结构类似于 iPod 应用程序,主要视图可以通过选择 TabBar 项目来查看,然后用户可以通过将视图推送到堆栈来使用 NavigationController 进一步深入.

Sorry for the newbie question. I have a UITabBar in my main window view as well as an array of UINavigationControllers for each Tab. The structure is similar to the iPod app in that the main views can be seen by selecting TabBar items and then the user can drill down further with the NavigationController by pushing views to the stack.

我想要做的是相当于从代码中的任何子视图按下底部的 TabBar 按钮(即,更改 TabBar 的 selected 属性并显示启动第一个视图控制器选项卡).

What I would like to be able to do is to do the equivalent of pressing a TabBar button at the bottom from any of the subviews in code (i.e., change the selected property of the TabBar and display launch the first view controller for the tab).

任何帮助将不胜感激.

戴夫

推荐答案

[myTabBarController setSelectedIndex:index]

从评论中回答第 2 部分的问题:

Answering the part 2 question from the comment:

您可以在 AppDelegate 中定义一个方法来切换到不同的选项卡.

You can define a method in AppDelegate for switching to a different tab.

您可以从任何地方获取 appdelegate 并发送消息..类似:

And you can get hold of appdelegate from anywhere and send a message.. something like:

 MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
 [appDelegate SwitchToTab:index]

523