简体   繁体   中英

TabBarController and Navigation Controller - Swift - Login

I am trying to add both a Navigation Bar Controller and a Tab Bar Controller to my application.

"First View Controller" is my first View Controller at index 0.

However, I cannot embed a Navigation Controller into First VC without incurring an error:

Could not cast value of type 'UINavigationController' (0x1a0a21fb8) to 'project.FirstViewController' (0x1000ae228).

Here's the code in my App Delegate:

 let TabBarController = self.window!.rootViewController as! UITabBarController
 let controller = TabBarController.viewControllers![0] as! FirstViewController
 controller.managedObjectContext = self.managedObjectContext

I realize that there is no Navigation Controller object in my code. Should the Tab Bar Controller NOT be behind the login page?

Obviously I plan on using both a Navigation Controller and Tab Bar Controller post-login.

But how do I instantiate a Navigation Controller to have a Navigation Bar in my First VC?

Thank you

Your Storyboard should look like this image 故事板

add navigation controller

this may help

You are actually almost there. From what I can read, the error states that it is unable to cast FirstViewController into UINavigationController . Assuming that your storyboard looks like the one in @PKT's answer, your codes should look like this.

let TabBarController = self.window!.rootViewController as! UITabBarController
let navigationController = TabBarController.viewControllers![0] as! UINavigationController
let controller = navigationController.topViewController as! FirstViewController
controller.managedObjectContext = self.managedObjectContext

The NavigationController is important if you want to NavigationBar. though.

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