Cody Harness
Cody Harness

Reputation: 1147

IBOutlet is nil in IBDesignable class

I have a custom view class that is supposed to just contain a label. I have an extension in each target that will provide the text that should be displayed in the view, this way I don't have recreate each controller just to have a different label and background color in it.

My custom code is:

@IBDesignable
class HomeAccentView: UIView {

    @IBOutlet weak var nameLabel: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)

        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setup()
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        setup()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()

        setup()
    }

    private func setup() {
        backgroundColor = UIColor.main

        nameLabel.text = String.headerText
        nameLabel.textColor = UIColor.white
    }
}

I have my nib's file owner as HomeAccentView and the label is connected as an outlet on the storyboard.nib storyboard

But whenever I try to set the text of the label it's always nil. What am I missing or doing wrong?

Upvotes: 3

Views: 583

Answers (1)

Luzo
Luzo

Reputation: 1374

You did not set class for view. You set only file owner, which means that you told that HomeAccessView is responsible for loading the view, but your code is not showing this. Is there any motive behind this? Otherwise, remove class from file owner and set it for view.

Upvotes: 3

Related Questions