How do I set a property on a custom view instantiated from a XIB

后端 未结 3 487
逝去的感伤
逝去的感伤 2021-01-29 01:22

I am just trying to get my head around MVC in Objective C and IOS but am having a problem I\'m hoping someone can help me with.

I have created a custom view (created as

3条回答
  •  攒了一身酷
    2021-01-29 02:08

    Example for setting corner radius of your custom subview (subclass of UIButton in my case) from xib.

    1. Create a property like this

      @property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
      
    2. Override setter in your custom view's implementation file.

      -(void)setCornerRadius:(CGFloat)cornerRadius
      {
           self.layer.cornerRadius = cornerRadius;
      }
      
    3. Drag your view in xib and change its class to your custom class.enter image description here

    4. Magic... You will see the custom properties appearing in attribute inspector like this. enter image description here

提交回复
热议问题