ios - 警告 : Attempt to present Tutorial_para_app. incialViewController on Tutorial_para_app.ViewControlle who view is not in the window hierarchy

标签 ios swift xcode

我希望当用户登录时,出现一个 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/

相关文章:

IOS:为标签组织动画

xcode - 如何添加xcode文本宏

ios - Swift:将 UIDatePicker 日期保存到 CoreData 中

ios - 启动屏幕的随机图像

ios - Storyboard中 uitableview 内的 Uicollectionview 在运行时变为 nil?

swift - 如何使用 swift combine 异步处理一系列任务

Swift:如何将信息传递到当前 View Controller 下方的 View Controller ?

iphone - (iOS) 如何查看 iPhone 已配对的蓝牙设备?

ios - 无法显示缓存目录中的 3d 文件

ios - UITextView 中的链接 : add and custom