Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segue in swift based on a logic

Tags:

ios

swift

segue

I want show a one of the two views in swift based on a if statement when the application first launches how do I do that this is the logic

if signupconfirmed == true {
// have to show one view 
} else {
// have to show another view 
}
like image 225
sriram hegde Avatar asked Feb 02 '26 17:02

sriram hegde


1 Answers

One way is you can initiate viewController with identifier with below code:

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 your 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 it will help you.

like image 176
Dharmesh Kheni Avatar answered Feb 05 '26 08:02

Dharmesh Kheni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!