Swift: how to show a tab bar controller after a login view

前端 未结 5 1098
抹茶落季
抹茶落季 2021-01-30 22:45

I have seen many posts similar to this here but they are all about Objective-C while I am developing my app in Swift. As you can see from the image I have a login screen view an

5条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 23:20

    To show tab bar controller from Login page, connect the Login page and TabbarController with a Show segue and give it an identifier in attributes inspector (Say "mySegueIdentifier").

    To add segue, just right click and drag from Login view controller to TabbarController.

    In successful Login you can simply call "performSegueWithIdentifier" method as follows

    self.performSegue(withIdentifier: "mySegueIdentifier", sender: nil)
    

    In your case you call it after this line.

    NSLog("Login OK")
    

    If you don't want to navigate from Login page to TabbarController, you can also set it as rootViewController after successful Login. To do this, set an identifier to TabbarController (Say "myTabbarController")

    let appDelegate = UIApplication.shared.delegate! as! AppDelegate
    let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "myTabbarControllerID")
    appDelegate.window?.rootViewController = initialViewController
    appDelegate.window?.makeKeyAndVisible()
    

    Edit:

    Swift 3

     let appDelegate = UIApplication.shared.delegate! as! AppDelegate
        
     let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "myTabbarControllerID") 
     appDelegate.window?.rootViewController = initialViewController
     appDelegate.window?.makeKeyAndVisible()
    

    Happy coding.. :)

提交回复
热议问题