How to insert a override function into a if else statement

故事扮演 提交于 2019-12-20 06:43:35

问题


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
    }
}

回答1:


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.

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

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:

Then select the type of 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)
    }
}

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

    }
}


来源:https://stackoverflow.com/questions/41527170/how-to-insert-a-override-function-into-a-if-else-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!