如何在if else语句中插入替代函数 [英] How to insert a override function into a if else statement

查看:124
本文介绍了如何在if else语句中插入替代函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到,使用基本逻辑,我无法将覆盖函数放入if else语句中,因为它将覆盖所有内容.但是,我仍然需要在if else语句中添加为segue做准备.因此,我正在使用的代码的工作方式是,如果用户两次按下按钮便赢得了游戏,因此转到显示分数的获奖者视图控制器.如果他们输了,他们将不加分数地转到视图控制器.因此,我需要将覆盖功能segue放在else if counter < 9.9 && level == 2部分的updateTimer()中.

I realize that with basic logic I cannot put a override function into a if else statement because it will override everything. However i still need to put in the if else statement the prepare for segue. So the way the code I am working works is if a user hits a button twice they win the game and therefore go to the winners view controller where a score is displayed. If they lose they go to a view controller with no score. So I need to put the override function segue in updateTimer(), In the else if counter < 9.9 && level == 2 part.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let DestViewController : winViewController = segue.destination as! winViewController

    DestViewController.LebelText = labelx.text!

}

func updateTimer() {
    counter += 0.1
    labelx.text = String(format: "%.1f", counter)
    if counter > 10 && level < 2 {
        let next = self.storyboard?.instantiateViewController(withIdentifier: "loseViewController") as? loseViewController
        self.present(next!, animated: true, completion: nil)
    } else if counter < 9.9 && level == 2 {
        let nextc = self.storyboard?.instantiateViewController(withIdentifier: "winViewController") as? winViewController
        self.present(nextc!, animated: true, completion: nil)
    } else {
        return
    }
}

推荐答案

要建立以上乔恩的注释,您甚至不应该调用present(UIViewController:)-它甚至不调用prepare(for segue:)方法.听起来您想要做的是在特定时间检查条件,然后根据该条件传递一些数据以呈现在目标视图控制器中.

To build off Jon's comment above, you shouldn't even be calling present(UIViewController:) - it won't even call the prepare(for segue:) method. It sounds like what you're trying to do is check a condition at a certain time, and based on that condition, pass along some data to present in the destination view controller.

如果您想使用segues,最好的方法是设置一个segue标识符:

If you want to use segues, the best idea is to set a segue identifier:

首先,您需要创建序列.将鼠标悬停在情节提要中VC顶部的视图控制器图标上,然后按住控件并拖动到目标VC:

First, you need to create the segue. Hover over the view controller icon at the top of the VC in the Storyboard, then hold control and drag to the destination VC:

然后选择segue的类型

Then select the type of segue

此后,您需要为序列设置一个唯一的标识符,以便可以区分代码中的任何其他序列.为此,选择segue本身,然后转到检查器窗格,然后在标识符"字段中键入一个唯一的名称:

After that you need to set a unique identifier for the segue so you can differentiate between any other segues in your code. To do this, select the segue itself and then go to the inspector pane and type a unique name in the "Identifier" field:

完成所需的两个序列后,就可以将代码编辑为以下内容:

After you've done that for both the segues you want, you can then edit your code to something like this:

func updateTimer() {
    counter += 0.1
    labelx.text = String(format: "%.1f", counter)
    if counter > 10 && level < 2 {
        // Use first unique segue identifier
        self.performSegue(withIdentifier: "identityA", sender: self)
    } else if counter < 9.9 && level == 2 {
        // Use second unique identifier
        self.performSegue(withIdentifier: "identityB", sender: self)
    }
}

现在,您可以在prepare(for segue:)中添加特殊代码,并且可以使用在Interface Builder中指定的唯一标识符来区分和添加针对不同目标VC的特殊代码

NOW you can add special code inside prepare(for segue:), and you can use the unique identifiers that you specified in Interface Builder to differentiate and add special code for the different destination VC's

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "identityA" {

        let destinationA: winViewController = segue.destination as! winViewController
        destinationA.LebelText = labelx.text

    } else if segue.identifier == "identityB" {

        let destinationB: loseViewController = segue.destination as! loseViewController
        destinationB.LebelText = labelx.text

    }
}

这篇关于如何在if else语句中插入替代函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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