简体   繁体   English

如何快速更改初始选项卡栏选择?

[英]How do I change the initial tab bar selection in swift?

In trying to find a solution I came across this answer: 在尝试找到解决方案时,我遇到了以下答案:

[ https://stackoverflow.com/a/10441200/4856759 ] [ https://stackoverflow.com/a/10441200/4856759 ]

It seems to accomplish what I want it to, but in objective C: 它似乎完成了我想要的,但是在目标C中:

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 0;

How do I do this in swift? 如何迅速做到这一点? My first attempt returns an error: 我的第一次尝试返回一个错误:

cannot convert the expression's type '$T4??' 无法转换表达式的类型'$ T4 ??' to type 'UITabBarController' 键入“ UITabBarController”

var tabBar: UITabBarController = self.window?.rootViewController
tabBar.selectedIndex = 1

Worked for me! 为我工作! You just need to add as! UITabBarController 您只需要添加as! UITabBarController as! UITabBarController : as! UITabBarController

var tabBar: UITabBarController = self.window?.rootViewController as! UITabBarController
tabBar.selectedIndex = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM