IBOutlet is nil Xcode 7 for ViewControllers

When I create a new subclass of the NSVC class and add some IBOulets to it, they are always zero when I try to access them. I tried creating a new project and only one VC class with only one output, and it is still zero.

I checked that they are really connected. I tried to instantiate a class with the full name nib. I also cleaned up the project and broke the data. Nothing has changed.

And what could be the reason for this? This is happening with all new projects; I added new stores to my old project and they work great. All IBOutlets are null, not just separate ones. I am on Xcode 7.1.1.

The error I am getting is:

fatal error: nil unexpectedly found while deploying optional value

+4
source share
2 answers

I found a job if someone is facing a similar problem. IBOutlet was zero because the superview was never loaded, so the application never created it. A simple hack writes this before using any subviews:

let view = viewController.view

This creates a view, therefore, the whole weekend on it.

PS I know that this is not a real solution, but I could not think of anything else. If anyone has received any suggestion, feel free to post messages.

+1
source

, init , , . .

- , , . , updateView() viewWillAppear(). , , , , , . , . :

class MyViewController: UIViewController {

    var product: Product {
        didSet {
            updateView()
        }
    }

    @IBOutlet private weak var productNameLabel: UILabel!

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        updateView()
    }

    private func updateView() {
        guard isViewLoaded() else {
            return
        }

        if let product = product {
            productNameLabel.text = product.title
        }
    }
}

, , , , . , , . , , , :

class MyViewController: UIViewController {

    var product: Product {
        didSet {
            loadViewIfNeeded()
            productNameLabel.text = product.title
        }
    }

    @IBOutlet private weak var productNameLabel: UILabel!
}
0

Source: https://habr.com/ru/post/1617572/


All Articles