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 button is an @IBOutlet:
@IBOutlet var button:UIButton!
The link in the storyboard is done.
I have nothing else in my app.
I got this error:
2017-03-26 20:31:44.935319+0200 ISAMG[357:47616] Failed to set (keyPath) user defined inspected property on (UIButton):
[<UIButton 0x15e3a780> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.
I don't understand what I'm missing.
EDIT 1
Following the advices @timaktimak, I created a custom UIButton class:
@IBDesignable
class ChoiceButton: UIButton {
@IBInspectable var keyPath: Bool! {
didSet {
print("didSet viewLabel, viewLabel = \(self.keyPath)")
}
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
}
The error is the same:
2017-03-26 22:32:27.389126+0200 ISAMG[429:71565] Failed to set (keyPath) user defined inspected property on (ISAMG.ChoiceButton):
[<ISAMG.ChoiceButton 0x14e8f040> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.
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!
Since this thread came up about 5 times in my search for a solution, I wanted to post my fix here for future frustrated souls. The reason I kept getting this error is because Interface Builder doesn't clean up old IBInspectable properties if you're experimenting with them.
In my case, I started with an Int property called type
and set a few custom buttons on my screen to use a value of 3 as a test. Later, I changed it to a more meaningful property name and made it a string, then set a few of those. But when I ran the app, I was getting this message.
I didn't discover the error until I went to the Identity Inspector and noticed that I still had an old keypath and value in use for some of the buttons:
Removing the old keypath fixed the problem.
In User Defined Runtime Attributes use layer.cornerRadius
instead of cornerRadius
.
Looks like failed to set keyPath
bool value to true of false.
setKeyPath to true or false!
来源:https://stackoverflow.com/questions/43032924/failed-to-set-user-defined-inspected-property-on-uibutton