关闭后如何以编程方式更改 UITabBar 所选索引? [英] How to programmatically change UITabBar selected index after Dismiss?

查看:28
本文介绍了关闭后如何以编程方式更改 UITabBar 所选索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITabBarViewcontroller 上有一个模态 UIViewController,我想关闭它,然后更改选项卡栏的所选项目.

I have a modal UIViewController over the UITabBarViewcontroller and i want to dismiss it, then change the selected item of my tab bar.

我试图通过在关闭完成中设置 selectedIndex 来完成它:

I'm trying to accomplish it by setting the selectedIndex inside dismiss' completion:

self.dismiss(animated: true, completion: {
    self.tabBarController?.selectedIndex = 2        
})

如果这是一个新手问题,我深表歉意,我在任何地方都找不到解决方案.预先感谢您发送给我的每个答案、线索或旧的类似问题:)

I apologize if this is a newbie question, i just couldn't find the solution to this anywhere. Thanks in advance for every answer, clue or old similar questions that you send me :)

推荐答案

我能够通过保存 presentingViewController(调用模态转场的视图控制器)的引用来解决这个问题关闭,然后使用它在完成中设置 selectedIndex .像这样:

I was able to solve this problem by saving the reference of the presentingViewController (the view controller who called the modal segue) before the dismiss, and then using it to set the selectedIndex inside the completion. Like this:

let referenceForTabBarController = self.presentingViewController as! UITabBarController
self.dismiss(animated: true, completion:{ 
     referenceForTabBarController.selectedIndex = 2
})

这篇关于关闭后如何以编程方式更改 UITabBar 所选索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆