简体   繁体   中英

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 ]

It seems to accomplish what I want it to, but in objective 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??' to type 'UITabBarController'

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

Worked for me! You just need to add as! UITabBarController as! UITabBarController :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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