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

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

我设计了嵌入了UINavigationController的LoginViewController。 我有一个带有2个viewcontrollers的UITabBarController。 我将LoginViewController设置为Storyboard中的inititialViewController。 因此loginview首次显示。 但是,我不知道如何从登录屏幕推送UITabBarController(登录按钮操作)。 此外,我不知道如何检查和加载登录和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) } 

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

 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 

只需在故事板中为UITabBarController提及“TabBarController”作为Identity。 我已经创建了一个嵌入了UINavigationController和UITabBarController的ViewController,它们分别带有三个UIViewControllers。

我希望它会帮助别人。 谢谢。

请检入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. }