storyboard instantiateViewControllerWithIdentifier not setting IBOutlets

前端 未结 6 1735
名媛妹妹
名媛妹妹 2020-12-08 19:22

I am using storyboard instantiateViewControllerWithIdentifier: and I\'m noticing that all the IBOutlets I have wired up are still nil. However, the

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 19:44

    People gave solution of how to get the view loaded (some of which you should never use (loadView()) so here is a way to check whether your view is already loaded

    I ran into the problem because I had a property with a didSet observer to update the UI which obviously doesn't work if the outlets are not yet set, so here is some example code how to work around it:

    var name: String? {
        didSet {
            updateNameLabel()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        updateNameLabel()
    }
    
    func updateRoomLabel() {
        guard isViewLoaded() else {
            return
        }
        [...]
    }
    

    So now when you display the outlets get updated but also every time you update the property

提交回复
热议问题