今天想做一個查找的程式,找到後頁面跳到内容處,發現設定了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