Swift 2: How to Load UITabBarController after Show

2019-04-13 08:27发布

问题:

Am new to Swift and Storyboard. At initially I have to show the login page and from the login page to UITabBarController. Once the user remembered the login details I have to check the login details in the AppDelegate and show the UITabBarController directly if the user already logged in. I have referred some SOF questions but, am not getting the result.

I designed the LoginViewController embedded with UINavigationController. And I have one UITabBarController with 2 viewcontrollers. I set the LoginViewController as a inititialViewController in Storyboard. So the loginview is showing at very first time. But, I don't know how to push the UITabBarController from the login screen (Login Button Action). Also I don't know how to check and load the login and tabbar

respectively from appDelegate.

Can anyone please help me? Thanks in advance.

@IBAction func loginButtonAction (button : UIButton) {

        if isRemeberLogin == true {
            let loginClass = LoginModelClass(userNameValue: (usernameTF?.text)!, passwordValue: (passwordTF?.text)!)
            print("Remembering Login Details: \(loginClass.userName, loginClass.passWord)")

        }

        let homeVC = self.storyboard?.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
        let collectionVC = self.storyboard?.instantiateViewControllerWithIdentifier("ItemsCollectionViewController") as! ItemsCollectionViewController


        //self.navigationController?.pushViewController(homeVC, animated: true)

        let tabBarController = self.storyboard?.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController

        print("TABBAR \(tabBarController)")
        let viewControllersArray = [homeVC, collectionVC];
       // tabBarController?.viewControllers = viewControllersArray

        self.navigationController?.pushViewController(tabBarController, animated: true)


    }

回答1:

Thank you for the answers. I resolved the issue and here is my code.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch

        let username = NSUserDefaults.standardUserDefaults().objectForKey("Username")
        print(username)

        let storyBoard: UIStoryboard = UIStoryboard(name:"Main", bundle: NSBundle.mainBundle())

        if username?.length > 0 {
            print("User already logged In")
            let tabBarController: UITabBarController = storyBoard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
            self.window?.makeKeyAndVisible()
            self.window?.rootViewController = tabBarController
        } else {
            print("New User")
            let loginViewController: ViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
            self.window?.makeKeyAndVisible()
            self.window?.rootViewController = loginViewController
        }


        return true
    }

This is from Login Button Action:

let storyboard: UIStoryboard = UIStoryboard(name:"Main", bundle: NSBundle.mainBundle())
        let tabBarController: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.window!.rootViewController = tabBarController

Just mention the "TabBarController" as Identity in Storyboard for the UITabBarController. I have created a Viewcontroller embedded with UINavigationController and UITabBarController with three UIViewControllers separately.

I hope it will help someone else. Thanks.



回答2:

Please check in image your storyboard must be look like following.

Then give Storyboard Segue identifier which navigate you to UITabbarController from LoginViewController.

Then paste following code from where you want to navigate.

@IBAction func btnLogin_Click(sender: AnyObject) {
    self.performSegueWithIdentifier("LoginToTabBar", sender: self)
    //LoginToTabBar is identifier which you settled out in storyboard segue.

}