简体   繁体   中英

Referencing TabbarController created from StoryBoard in AppDelegate?

I have a tabbar app with an initial login screen. The tabbarController is set as the initial view in Storyboard with 1 VC that has a navigationController also embed.

I have a loginVC instantiated and set as rootViewController in my AppDelegate. After the user has successfully sign in from the loginVC, I need to switch over to the tabbarController. Would I try to get a reference to the tabbarcontroller and set it as the new rootviewcontroller? If so, I'm having a hard time figuring out how to do this:/

AppDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if NSUserDefaults.standardUserDefaults().objectForKey("OAuth") == nil {

        self.window = UIWindow(frame:UIScreen.mainScreen().bounds)

        var storyboard = UIStoryboard(name: "Main", bundle: nil)

        var loginVC = storyboard.instantiateViewControllerWithIdentifier("LOGIN_VC") as LoginVC


        self.window?.rootViewController = loginVC
        self.window?.makeKeyAndVisible()
    } 

    return true
}

This method gets called after user has successfully signed in

  func dismissLoginVC() {
    var tabbarController = self.storyboard?.instantiateViewControllerWithIdentifier("TABBAR") as UITabBarController

    self.presentViewController(tabbarController, animated: true, completion: nil)

    let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    appDelegate.window?.rootViewController =  tabbarController

}

I know the problem with this is it just created a new tabbarcontroller, rather than referencing the existing tabbarController that was set as the initialView in storyboard. This seems to work but it is missing other items from the navigation bar.

Thanks for pointing me in the right direction!

I think you should change your app structure. Keep the tab bar controller as the initial view controller in the storyboard, and present modally (with no animation) the login controller from the viewDidAppear method of the controller in the first tab -- it will be the first thing the user sees. When you dismiss it, you will be back to that controller in the first tab. With this approach, you don't need any code in the app delegate.

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