UITabBarController的selectedIndex属性不起作用?

今天想做一个查找的程序,找到后页面跳到内容处,发现设置了UITabBarController的selectedIndex属性不起作用。找到了原因,记录下来:

UITabBarController默认显示Index=0的页面,如果要想在程序启动时就指定显示的页面,tabBar.selectedIndex一定要写在tabBar.viewControllers之后,也就是说把页面序列加进TabBarController之后才能指定显示页。


如果是程序加载完成后要做TabBarController之间页面跳转,需要这么写:

let appDelegate = (UIApplication.shared.delegate) as! AppDelegate
let tabBar = appDelegate.window!.rootViewController as! UITabBarController

tabBar.selectedIndex = i


你可能感兴趣的:(Swift)