3

Even though my property is viewable in IB’s property inspector when I select the appropriate view, the property’s didSet method does not update the relevant subview’s.

Here is my property:

@IBInspectable var imageRepresentation: UIImage = UIImage() {
    didSet {
        let image = imageRepresentation
        let hv = self.headerImageView
        let bv = self.backgroundImageView

        self.headerImageView.image = imageRepresentation
        self.backgroundImageView.image = imageRepresentation
    }
}

As you can see, I’m expecting two UIImageView’s to be updated to the new image. However, when I use the Editor > Debug Selected Views and set a breakpoint, each of my views are nil.

I’d love to hear anyones ideas.

Thanks

Adam Carter
  • 4,741
  • 5
  • 42
  • 103

2 Answers2

1

It seems that all IBOutlet will not be initialized in IBDesignable views before some time. But IB will access those IBOutlet since you need to set/get some attributes of them. One way is to make all of them optional like

self.headerImageView?.image = imageRepresentation

And then when you are sure the IBDesignable view will show up on the screen (which means that all the subviews are already initialized), manually set the property again.

The other way is to create all the subviews of your IBDesignable view from code. In this way you do not need any IBOutlet, and everything should be there in anytime.

Wizard
  • 389
  • 1
  • 2
  • 12
0

Instead of making a property observer on the @IBInspectable try make a property observer on the @IBOutlet. See this post from Natasha the robot. For example:

@IBInspectable var buttonTitle: String = ""
@IBOutlet weak var titleLabel: UILabel! {
    didSet {
        titleLabel.text = buttonTitle
    }
}
Samuël
  • 1,147
  • 1
  • 10
  • 17