Failed to set () user defined inspected property on (UIButton)

后端 未结 6 1206
青春惊慌失措
青春惊慌失措 2021-01-17 09:50

In a simple ViewController, I added one UIButton. I would like to use \"User Defined Runtime Attributes\". For now I added the default Bool attribute.

The b

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-17 10:26

    Well, your button doesn't have a property called keyPath to be setting it to something.

    User Defined Runtime Attributes are used to simply set a property value in the Interface Builder.

    You can use a standard property that every UIButton has, for example backgroundColor:

    Or, you can create a custom UIButton subclass, add a property to it, then set the button's class to the created custom subclass and set the property value in the User Defined Runtime Attributes section.

    You can check out, for example, this answer, it contains an example of a custom class with User Defined Runtime Attributes: https://stackoverflow.com/a/24433125/3445458

    Edit: make sure you are not using optional (or implicitly unwrapped optional) for the type, it doesn't work with User Defined Runtime Attributes (I guess because the Interface Builder doesn't know whether the value can be set to nil, and so show it in the options or not).

    So you can't do

    var keyPath: Bool!
    

    instead you can only do

    var keyPath: Bool = false
    

    or don't use a default value and set it in the constuctor instead. For some reason it works with an optional String (and in the example they use an optional String), but it doesn't with optional Bool. To conclude, don't use or worry about User Defined Runtime Attributes too much, it is clearer to set the default values in code!

    Hope this helps! Good luck!

提交回复
热议问题