Go fast based on logic

I want to show one of the two views in swift based on an if statement when the app first starts up, how do I do this is the logic

if signupconfirmed == true {
// have to show one view 
} else {
// have to show another view 
}

      

+3


source to share


2 answers


In one case, you can initiate a viewController with an id with the code below:

var signupconfirmed = true
@IBAction func signUpPressed(sender: AnyObject) {

    if signupconfirmed {
        // have to show one view
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("First") as! SViewController
        self.presentViewController(vc, animated: true, completion: nil)
    } else {
        // have to show another view
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("Second") as! TViewController
        self.presentViewController(vc, animated: true, completion: nil)
    }
}

      

Update:

You can perform this action in AppDelegate.swift



Here is your code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let signupconfirmed = NSUserDefaults.standardUserDefaults().boolForKey("SignUp")
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    var initialViewController = UIViewController()
    var storyboard = UIStoryboard(name: "Main", bundle: nil)
    if signupconfirmed {
        initialViewController = storyboard.instantiateViewControllerWithIdentifier("First") as! UIViewController
    } else{
        initialViewController = storyboard.instantiateViewControllerWithIdentifier("Second") as! UIViewController
    }

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
    return true
}

      

Hope this helps you.

+3


source


in appDelegate "didFinishLaunchingWithOptions" before returning



 var userSignedUp = NSUserDefaults.standardUserDefaults().boolForKey("signup")

        if userSignedUp {
               // have to show another view
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("anyOtherViewThanSignUp") as! TViewController
        self.window?.rootViewController =  vc
        } else {
             // have to show SignUp view
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("signupView") as! SViewController
        self.window?.rootViewController =  vc

        }
    }

      

+1


source







All Articles