Ios 在应用程序启动时为UITabBarController选择大于3的索引不工作
在应用程序启动时,我将UITabBarController作为根视图控制器。对于这个选项卡栏,我总共有7个ViewController。在方法Ios 在应用程序启动时为UITabBarController选择大于3的索引不工作,ios,swift,swift2,uitabbarcontroller,Ios,Swift,Swift2,Uitabbarcontroller,在应用程序启动时,我将UITabBarController作为根视图控制器。对于这个选项卡栏,我总共有7个ViewController。在方法应用程序中的AppDelegate.swift内部(应用程序:didfishlaunchingwithoptions:),如果我执行以下操作,它将正常工作: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:
应用程序中的AppDelegate.swift
内部(应用程序:didfishlaunchingwithoptions:)
,如果我执行以下操作,它将正常工作:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.tabBarController = self.window?.rootViewController as! UITabBarController
self.tabBarController.selectedIndex = 2
return true
}
这非常有效,当应用程序启动时,选项卡栏中的第三项被选中
但是,当我想选择在更多导航控制器下的任何项目时(这意味着超出3的任何索引),self.tabBarController.selectedIndex=5
根本不起作用。上述语句在应用程序启动完成后的任何部分都有效,即,如果我在第一个ViewController的ViewDidDisplay
中执行self.tabBarController.selectedIndex=5
,则该语句有效;但它在应用程序启动时不起作用,即在应用程序中(应用程序:didFinishLaunchingWithOptions:)
。启动后,选项卡栏中选定的项将保留为默认项。在应用程序启动时,我可以通过什么方式将所选索引更改为3(位于moreNavigationController
下)?尝试添加
// Override point for customization after application launch.
self.tabBarController = self.window?.rootViewController as! UITabBarController
self.tabBarController.selectedIndex = 2
self.tabBarController.view.layoutIfNeeded()
return true
如果您使用的是故事板。答案对我有用。你可以在tabBarController类中创建一个可检查的var,并在故事板中指定索引号。@Leo Dabus,我想处理应用程序启动时推送通知的深层链接。因此,如果我的应用程序未运行且收到推送通知,我会单击推送通知。此时会触发
应用程序(应用程序:didFinishLaunchingWithOptions:)
,但第一视图控制器尚未加载,因此在视图控制器实例不可观察的情况下,我的观察者此时将如何工作?我尝试了您的建议。不幸的是,它不起作用:(试试这个