How to insert a override function into a if else statement

后端 未结 1 803
一个人的身影
一个人的身影 2021-01-27 10:55

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 stateme

相关标签:
1条回答
  • 2021-01-27 11:02

    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
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题