如何更改 swift 中的初始选项卡栏选择?
How do I change the initial tab bar selection in swift?
在寻找解决方案时,我遇到了这个答案:
[]
它似乎完成了我想要的,但在 objective C:
UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 0;
如何在 swift 中执行此操作?我第一次尝试returns出错:
cannot convert the expression's type '$T4??' to type 'UITabBarController'
var tabBar: UITabBarController = self.window?.rootViewController
tabBar.selectedIndex = 1
对我有用!您只需要添加 as! UITabBarController
:
var tabBar: UITabBarController = self.window?.rootViewController as! UITabBarController
tabBar.selectedIndex = 1
在寻找解决方案时,我遇到了这个答案:
[]
它似乎完成了我想要的,但在 objective C:
UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 0;
如何在 swift 中执行此操作?我第一次尝试returns出错:
cannot convert the expression's type '$T4??' to type 'UITabBarController'
var tabBar: UITabBarController = self.window?.rootViewController
tabBar.selectedIndex = 1
对我有用!您只需要添加 as! UITabBarController
:
var tabBar: UITabBarController = self.window?.rootViewController as! UITabBarController
tabBar.selectedIndex = 1