如何根据快速单击按钮的条件执行不同的转场? [英] How to perform different segue based on condition from a button click in swift?

查看:21
本文介绍了如何根据快速单击按钮的条件执行不同的转场?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,如果您第一次打开该应用程序,其中一个按钮会将您带到个人资料注册表视图,如果您在其他时间打开该应用程序,则进入另一个.我如何以编程方式完成此操作,我所看到的所有内容仅将按钮连接到一个视图.

I'm building an app in which a button takes you to a profile registry view if you open the app for the first time and into another if you open the app any other time. How can I accomplish this programmatically, everything i have seen only connects the button to one view.

@IBAction func NextViewController(_ sender: UIButton) {
    if launchedBefore  {
        print("Not first launch.")

    } else {
        print("First launch, setting UserDefault.")


        UserDefaults.standard.set(true, forKey: "launchedBefore")
    }
}

推荐答案

我想,您是在尝试连接按钮以使用多个 ViewController 执行 segue 对吗?这是不可能的

I think, You are trying to connect the button to perform segue with multiple ViewController Right?? which is not possible

相反,您必须在视图控制器之间连接 segue

Instead that you have to connect segue between View Controllers

在两个 viewController 之间添加一个 segue:

在 Interface Builder 中,按住 ctrl + 在要链接的两个 viewController 之间拖动.你应该看到:

From the Interface Builder, ctrl + drag between the two viewControllers that you want to link. You should see:

现在根据您的条件,您应该使用如下标识符执行 segue:

And now based on you condition you should perform segue with identifiers like below:

@IBAction func NextViewController(_ sender: UIButton) {
            if launchedBefore  {
        /*Use the Identifier you given in story Board*/
               self.performSegue(withIdentifier: "otherVC", sender: self)

            } else {
    /*Use the Identifier you given in story Board*/
        self.performSegue(withIdentifier: "register", sender: self))
                UserDefaults.standard.set(true, forKey: "launchedBefore")
            }
        }

有关 segue 的更多描述性答案,请参阅答案 如何将两个视图控制器连接到故事板中的一个按钮?

For more Descriptive answer about segue see the answer How to connect two view controllers to one button in storyboard?

这篇关于如何根据快速单击按钮的条件执行不同的转场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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