Swift 2:显示LoginViewController后如何加载UITabBarController

Swift 2: How to Load UITabBarController after Showing LoginViewController

我是 Swift 和 Storyboard 的新手。最初我必须显示登录页面并从登录页面显示到 UITabBarController。一旦用户记住了登录详细信息,我必须检查 AppDelegate 中的登录详细信息,如果用户已经登录,则直接显示 UITabBarController。我已经提到了一些 SOF 问题,但没有得到结果。

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

分别来自appDelegate。

谁能帮帮我?提前致谢。

@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)


    }

请检查 image 您的故事板必须如下所示。

然后提供 Storyboard Segue 标识符,它将您从 LoginViewController 导航到 UITabbarController

然后 paste 按照您要导航的代码进行导航。

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

}

感谢您的回答。我解决了这个问题,这是我的代码。

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
    }

这来自登录按钮操作:

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

只需提及 "TabBarController" 作为 UITabBarController 故事板中的标识。我创建了一个 Viewcontroller 嵌入了 UINavigationController 和 UITabBarController 的三个 UIViewControllers。

希望对其他人有所帮助。谢谢