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

查看:214
本文介绍了无法在(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.

我不明白我在想什么.

编辑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.总之,不必过多使用或担心用户定义的运行时属性,在代码中设置默认值会更加清楚!

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天全站免登陆