IBOutlet is not initialized

纵饮孤独 提交于 2019-12-11 14:09:18

问题


there is something strange with xcode. Suddenly the ordinary flow is broken. To check it I've created a new project with just two viewControllers. First (named ViewController) contains only one button to open the second (named NewViewController) controller, which contains the only UILabel.

import UIKit

class ViewController: UIViewController {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "Show New View" {
            guard let newVC = segue.destination as? NewViewController else { return }

            newVC.passedText = "some abstract text"
        }
    }
}

import UIKit

class NewViewController: UIViewController {
    @IBOutlet weak var myLabel: UILabel!

    var passedText: String? {
        didSet {
            guard let text = passedText else { return }

            myLabel.text = text
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

the main.storyboard looks so

IBOutlet is well connected

But the debugger shows what the UILabel is nil

What I've made wrong?


回答1:


No, nothing has "broken". You're the one who is doing it wrong. At the time you are calling newVC.passedText = "some abstract text", newVC has not yet loaded its view and its outlets are still nil. So the call to set myLabel.text is happening at time when the outlet is indeed still nil.

Your mistake is that you are trying to configure myLabel too soon. Store the value in newVC, yes. But as for setting myLabel.text, wait until viewDidLoad, when the view and outlets are working.

The correct pattern, therefore, looks like this:

var passedText: String? {
    didSet {
        guard let text = passedText else { return }
        myLabel?.text = text
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    guard let text = passedText else { return }
    myLabel?.text = text
}

Now your code does the right thing regardless of when passedText is set in relation to viewDidLoad.




回答2:


You need to set the custom class NewViewController in the identity inspector of the view controller in the storyboard. Maybe you forgot to do that?



来源:https://stackoverflow.com/questions/48671671/iboutlet-is-not-initialized

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