无法在 (UIButton) 上设置 () 用户定义的检查属性 [英] Failed to set () user defined inspected property on (UIButton)

查看:10
本文介绍了无法在 (UIButton) 上设置 () 用户定义的检查属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个简单的 ViewController 中,我添加了一个 UIButton.我想使用用户定义的运行时属性".现在我添加了默认的 Bool 属性.

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.

按钮是一个@IBOutlet:

The button is an @IBOutlet:

@IBOutlet var button:UIButton!

故事板中的链接已完成.

The link in the storyboard is done.

我的应用中没有其他内容.

I have nothing else in my app.

我收到了这个错误:

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.

编辑 1

按照@timaktimak 的建议,我创建了一个自定义 UIButton 类:

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)!
    }
}

错误是一样的:

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.

推荐答案

嗯,你的按钮没有一个名为 keyPath 的属性来设置它.

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

用户定义的运行时属性用于在 Interface Builder 中简单地设置属性值.

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

您可以使用每个 UIButton 都有的标准属性,例如 backgroundColor:

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

或者,您可以创建自定义 UIButton 子类,为其添加属性,然后将按钮的类设置为创建的自定义子类,并在用户定义的运行时属性部分设置属性值.

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.

例如,您可以查看此答案,它包含具有用户定义的运行时属性的自定义类的示例:https://stackoverflow.com/a/24433125/3445458

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

确保您没有使用可选(或隐式展开的可选)类型,它不适用于用户定义的运行时属性(我猜是因为 Interface Builder 不知道该值是否可以设置为 nil,并在选项中显示).

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).

所以你不能这样做

var keyPath: Bool!

你只能这样做

var keyPath: Bool = false

或者不要使用默认值,而是在构造函数中设置它.出于某种原因,它与可选的 String 一起使用(在示例中,他们使用可选的 String),但它不适用于可选的 Bool.总而言之,不要过度使用或担心 User Defined Runtime Attributes,在代码中设置默认值更清楚!

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!

这篇关于无法在 (UIButton) 上设置 () 用户定义的检查属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆