How to set a property on a custom view created using XIB

I'm just trying to bow my head around MVC in Objective-C and IOS, but I have a problem. I hope someone can help me.

I created a custom view (created as a child UIView in XIB) that uses a simple delegate protocol to request information from my delegate in drawRect. I have a view controller that implements the protocol and connects to the view through the interface builder. The user view also has several properties that I want to set at startup. My problem is how the controller needs to access the view in order to set these properties, since it does not have direct access to it. Also, the properties do not seem visible in the inspector of the interface constructor, as I would expect, unlike the delegation property that I added.

At first I thought I could do something like

[self.view setViewIntProperty:10]

But this will be a call to the main XIB view, and my custom view is actually a child of this view, so I need to somehow get this child view so that I can initialize it from the controller in viewDidLoad.

I hope this is clear. I am sure that this should be easy, and I missed something simple, but I can’t understand how it works normally.

0
source share
3 answers

Custom properties of the view (unlike its access points) can be set only in the code, if you do not create an IB-plugin for it .

, . . / . . ( Interface Builder) . .

Interface Builder, , IB. , , .

+1

MyCustomView *.

IBOutlet, IB.

.

+3

( UIButton ) xib.

  • ,

    @property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
    
  • Replace setter in your custom view implementation file.

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

  • Magic ... You will see custom properties appearing in the attribute inspector as follows. enter image description here

0
source

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


All Articles