简体   繁体   中英

Set rootViewController based on condition

I am currently working on an application in Xcode 7.2, iOS 9.2 using Swift. I want to have a screen pop up the very first time the user opens the application and get some data and store it permanently. I found a thread where someone asked something similar: Programmatically set the initial view controller using Storyboards have followed the instructions closely, but I can't seem to get it to work the way I want it to.

Here is the modified code from my AppDelegate.swift file.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds);
    let storyboard = UIStoryboard(name: "Main", bundle: nil);
    var initialViewController: UIViewController;
    print(NSUserDefaults.standardUserDefaults().boolForKey("firstTime"));
    if(!NSUserDefaults.standardUserDefaults().boolForKey("firstTime")){
        initialViewController = storyboard.instantiateViewControllerWithIdentifier("FirstTimeScreen");
    } else {
        initialViewController = storyboard.instantiateViewControllerWithIdentifier("Dashboard");
    }
    self.window?.rootViewController = initialViewController;
    self.window?.makeKeyAndVisible();
    return true
}

In the ViewController.swift file corresponding to the ViewController that should open from the second time onwards, in the viewDidLoad() method, I have the following line of code:

NSUserDefaults.standardUserDefaults().setBool(false, forKey: "firstTime");

Currently the first screen is always opening. The screen which should from the second time onwards is not the root view controller even though the NSUserDefaults value is tested.

I started the application working on a single view controller, and I currently do not have any Navigation Controllers or anything. Just a bunch of individual View Controllers, connected through button segues.

Thank you for taking the time to read this and possibly answer the question.

Looking closely at your logic here, there is a ! in the if statement, which means if firstTime is not true it will show the view controller with identifier FirstTimeScreen . You'll want to remove the !, so that it will show the FirstTimeScreen when it is the first time launching.

Be sure that you store false for firstTime in the FirstTimeScreen , not Dashboard .

Also be sure you register NSUserDefaults with firstTime set to true , otherwise it will be false and never show your FirstTimeScreen .

You need to set "firstTime" to true, instead of false: NSUserDefaults.standardUserDefaults().setBool(false, forKey: "firstTime");

Also, make sure you use NSUserDefaults.standardUserDefaults().synchronize() after that to save the changes.

Try to set firstTime in the didFinishLaunchingWithOptions

    if(NSUserDefaults.standardUserDefaults().objectForKey("firstTime") == nil){
        initialViewController = storyboard.instantiateViewControllerWithIdentifier("FirstTimeScreen");
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "firstTime");
        NSUserDefaults.standardUserDefaults().synchronize()
    } else {
        initialViewController = storyboard.instantiateViewControllerWithIdentifier("Dashboard");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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