警告:尝试在Tutorial_para_app.ViewControlle上显示Tutorial_para_app.foreViewController,其视图不在窗口层次结构中

时间:2016-08-21 22:23:55

标签: ios swift xcode

我希望当用户登录时,出现一个视图控制器,如果他没有登录,我想要另一个视图控制器...基本上我想在用户登录时更改初始视图控制器。但是当我尝试我正在做的事情时,我会收到标题中出现的错误。

我搜索了这个问题,我尝试了答案,但它对我不起作用..

这是我的......

 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的故事板中

感谢。

1 个答案:

答案 0 :(得分:0)

要在启动应用程序时修改Windows根视图控制器,您可以执行以下操作:

- (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;
}

这会在应用程序加载时交换窗口的根视图控制器,具体取决于它是否是第一次运行。

如果您不知道如何从故事板或xib加载视图控制器,请进行快速搜索。有很多如何做的例子。