我希望当用户登录时,出现一个 View Controller ,如果他没有登录,我想要另一个 View Controller ...基本上我想在用户登录时更改初始 View Controller 。但是当我尝试我正在做的事情时,我得到了标题中出现的错误。
我搜索了这个问题并尝试了答案,但它对我不起作用..
这是我的..
let defaults = NSUserDefaults.standardUserDefaults()
if defaults.boolForKey("FirstLaunch") {
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Normal") as UIViewController
self.presentViewController(viewController, animated: true, completion: nil)
}
else {
defaults.setBool(true, forKey: "FirstLaunch")
defaults.synchronize()
}
我也试过把这个...
let top:UIViewController = (UIApplication.sharedApplication().keyWindow?.rootViewController)!
top.presentViewController(viewController, animated: true, completion: nil)
但没有解决问题。 所有这些代码都在我的 ViewController.swift 的 Storyboard中
谢谢。
最佳答案
要在启动应用程序时修改 Windows Root View Controller ,您可以执行以下操作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL firstRun = <However you figure out if it's the apps first run>
if(firstRun){
self.window.rootViewController = <Load your first run controller from storyboard or xib>
} else {
self.window.rootViewController = <Load your normal controller from storyboard or xib>
}
[self.window makeKeyAndVisible];
...
return YES;
}
这将在应用加载时交换窗口的 Root View Controller ,具体取决于它是否是第一次运行。
如果您不知道如何从 Storyboard或 xib 加载 View Controller ,请快速搜索。有很多关于如何做到这一点的例子。
关于ios - 警告 : Attempt to present Tutorial_para_app. incialViewController on Tutorial_para_app.ViewControlle who view is not in the window hierarchy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39069375/