基于逻辑的SWIFT分段 [英] Segue in swift based on a logic

查看:15
本文介绍了基于逻辑的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分段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆