以编程方式切换到 TabBar 选项卡视图?
假设我的 iPhone 应用程序的一个选项卡视图中有一个 UIButton
,我想让它在 TabBarController
的选项卡栏中打开另一个选项卡。我该如何编写代码来做到这一点?
我假设我卸载现有视图并加载特定的选项卡视图,但我不确定如何准确编写代码。
Let's say I have a UIButton
in one tab view in my iPhone app, and I want to have it open a different tab in the tab bar of the TabBarController
. How would I write the code to do this?
I'm assuming I unload the existing view and load a specific tab view, but I'm not sure how to write the code exactly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
在 Swift 或 Objective-C 中尝试此代码
Swift
Objective-C
Try this code in Swift or Objective-C
Swift
Objective-C
请注意,选项卡的索引从 0 开始。因此,以下代码片段
适用于栏中的第五个选项卡。
Note that the tabs are indexed starting from 0. So the following code snippet works
goes to the fifth tab in the bar.
我的观点是
selectedIndex
或使用objectAtIndex
不一定是切换选项卡的最佳方式。如果您重新排序选项卡,硬编码的索引选择可能会扰乱您以前的应用程序行为。如果您有要切换到的视图控制器的对象引用,您可以这样做:
当然您必须确保
myViewController
确实在tabBarController.viewControllers
列表中代码>.My opinion is that
selectedIndex
or usingobjectAtIndex
is not necessarily the best way to switch the tab. If you reorder your tabs, a hard coded index selection might mess with your former app behavior.If you have the object reference of the view controller you want to switch to, you can do:
Of course you must make sure, that
myViewController
really is in the list oftabBarController.viewControllers
.您只需将 UITabBarController 上的
selectedIndex
属性设置为适当的索引,视图就会发生变化,就像用户点击选项卡按钮一样。You can simply just set the
selectedIndex
property on the UITabBarController to the appropriate index and the view will be changed just like the user tapped the tab button.我尝试了 Disco S2 建议的方法,它很接近,但这就是最终对我有用的方法。这是在另一个选项卡内完成操作后调用的。
I tried what Disco S2 suggested, it was close but this is what ended up working for me. This was called after completing an action inside another tab.
类似于 Stuart Clark 的解决方案,但对于 Swift 3:
像这样使用它:
请注意,我的 tabController 链接到 navigationControllers 后面的 viewControllers。如果没有navigationControllers,它看起来像这样:
Like Stuart Clark's solution but for Swift 3:
Use it like this:
Please note that my tabController links to viewControllers behind navigationControllers. Without navigationControllers it would look like this:
我的问题有点不同,我需要从第一个 tabBar 中的一个 childViewController 切换到第二个 tabBar 的 home viewController 。我只是使用楼上提供的解决方案:
但是当它切换到第二个tabBar的主页时,内容是不可见的。当我调试时,viewDidAppear,viewWillAppear,viewDidLoad,它们都没有被调用。
我的解决方案是在 UITabBarController 中添加以下代码:
My issue is a little different, I need to switch from one childViewController in 1st tabBar to home viewController of 2nd tabBar. I simply use the solution provided in the upstairs:
However when it switched to the home page of 2nd tabBar, the content is invisible. And when I debug, viewDidAppear, viewWillAppear, viewDidLoad, none of them is called.
My solutions is to add the following code in the UITabBarController:
对于您可能移动选项卡的情况,这里有一些代码。
For cases where you may be moving the tabs, here is some code.
我希望能够指定按类而不是索引显示哪个选项卡,因为我认为它是一个强大的解决方案,不太依赖于连接 IB 的方式。我没有找到 Disco 或 Joped 的解决方案,所以我创建了这个方法:
你这样称呼它:
希望这对某人有帮助
I wanted to be able to specify which tab was shown by class rather than index as I thought it made for a robust solution that was less dependant on how you wire up IB. I didn't find either Disco's or Joped's solutions to work so i created this method:
you call it like this:
Hope this is helpful to someone
在
AppDelegate.m
文件中使用:Use in
AppDelegate.m
file:类似于 Stuart Clark 的解决方案,但对于 Swift 3 并使用恢复标识符来查找正确的选项卡:
像这样使用它(还必须在故事板中为特定 viewController 设置“人类”和“机器人”及其恢复 ID,或者使用故事板 ID 并检查“使用storyboard ID”作为恢复ID):
请注意,我的tabController链接到navigationControllers后面的viewControllers。如果没有navigationControllers,它看起来像这样:
Like Stuart Clark's solution but for Swift 3 and using restoration identifier to find correct tab:
Use it like this ("Humans" and "Robots" must also be set in storyboard for specific viewController and it's Restoration ID, or use Storyboard ID and check "use storyboard ID" as restoration ID):
Please note that my tabController links to viewControllers behind navigationControllers. Without navigationControllers it would look like this: