基于逻辑思考swift
我希望在应用程序首次启动时基于if语句在swift中显示两个视图中的一个,我该怎么做才是这个逻辑
if signupconfirmed == true { // have to show one view } else { // have to show another view }
一种方法是你可以使用以下代码启动带有标识符的viewController:
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) } }
更新:
您可以在AppDelegate.swift
执行此操作
这是你的代码:
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 }
希望它会对你有所帮助。
在你的appDelegate“didFinishLaunchingWithOptions”之前返回
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 } }