Passing variable back to parent in Swift

前端 未结 2 437
礼貌的吻别
礼貌的吻别 2021-02-08 14:28

I am re-writing a tutorial converting the code from Objective-C to swift. The app moves from VC one where there is 3 sliders (Red, Green and Blue) that set the background colour

2条回答
  •  感情败类
    2021-02-08 15:21

    It's possible that the view controller returned by ViewController() has not yet loaded its views. You could try checking this in a setter function and storing it for later use once the views have been loaded.

    class VC : UIViewController {
        @IBOutlet weak var colourLabel: UILabel!
        var savedLabelText: String?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.colourLabel.text = self.savedLabelText
        }
    
        func setColorLabelText(label: String) {
            if self.isViewLoaded() {
                self.colourLabel.text = label
            }
            else {
                self.savedLabelText = label
            }
        }
    }
    

提交回复
热议问题