Segue in swift based on a logic(基于逻辑的SWIFT分段)
问题描述
我希望在应用程序首次启动时,基于If语句在SWIFT中显示两个视图中的一个如何实现 这就是逻辑if signupconfirmed == true {
// have to show one view
} else {
// have to show another view
}
推荐答案
一种方法是,您可以使用以下代码的标识符来初始化视图控制器:
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
}
希望它能对您有所帮助。
这篇关于基于逻辑的SWIFT分段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!