Segue in swift based on a logic

六眼飞鱼酱① 提交于 2019-12-22 11:25:30

问题


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 
}

回答1:


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.




回答2:


in your appDelegate "didFinishLaunchingWithOptions" before return

 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

        }
    }


来源:https://stackoverflow.com/questions/31289462/segue-in-swift-based-on-a-logic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!