简体   繁体   English

在没有警告的情况下从登录故事板移动到Swift中的主故事板

[英]Moving from a login Storyboard to the main Storyboard in Swift without Warning

I am trying to change view controller after a user logs into my app from the login storyboard to my main storyboard, this works successfully but I receive the following warning in the console: 当用户从登录情节提要登录到我的主情节提要后,我试图更改视图控制器,此操作成功完成,但在控制台中收到以下警告:

2016-02-05 01:57:59.553 Commu[4749:160489] Warning: Attempt to present <UITabBarController: 0x7f93285b9d90> on <Commu.LoginViewController:     0x7f9328494e80> whose view is not in the window hierarchy!

The code in my login view controller is as follows: 我的登录视图控制器中的代码如下:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   let vc = storyboard.instantiateViewControllerWithIdentifier("MainMenu")
   self.presentViewController(vc, animated: true, completion: nil)                         
})

Is there a better way to move between storyboards to avoid this warning message? 是否有更好的方式在情节提要之间移动,以避免出现此警告消息?

Once user login, login hierarchy no more required. 一旦用户登录,就不再需要登录层次结构。

After successful login your need to change root view controller of your mainWindow, ie rootViewContoller of window of appDelegate. 成功登录后,您需要更改mainWindow的根视图控制器,即appDelegate窗口的rootViewContoller。

Similarly change the rootViewContoller of window again when user logout. 同样,当用户注销时,再次更改window的rootViewContoller。

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MainMenu")


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

You can also include UINavigationController in hierarchy, to push and pop other viewControllers. 您还可以在层次结构中包括UINavigationController,以推送和弹出其他viewController。

The warning is because: 该警告是因为:

self.presentViewController(vc, animated: true, completion: nil) is presenting the viewController as a modal view. self.presentViewController(vc, animated: true, completion: nil)将viewController呈现为模式视图。 Which should only be used to display a screen for a short period of time to give the user some actions. 只能在短时间内显示屏幕,以便为用户提供一些操作。 It is intended that the user will, at some point, move back from the modal window. 预期用户将在某个时候从模态窗口移回。

There is a new feature, called a storyboard reference . 有一个新功能,称为情节提要参考 This allows you to use a segue between storyboards. 这使您可以在情节提要之间使用序列。

Then you could use: 然后,您可以使用:

self.performSegueWithIdentifier("segueId", sender: self)

This is a much cleaner way to do it and keeps all your navigation controls inside the storyboard. 这是一种更清洁的方法,可将所有导航控件保留在情节提要中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM