简体   繁体   English

Swift:如何实现用户定义的运行时属性

[英]Swift: How to implement User Defined Runtime Attributes

So I am implementing a custom "chooser" toolbar, like the iOS equivalent of a radio button set ( UISegmentedControl ). 所以我正在实现一个自定义的“选择器”工具栏,就像iOS等效的单选按钮集( UISegmentedControl )。 Just a horizontal bar divided into options. 只是一个单杠分为选项。

To do this, I created a subclass of UIControl called SegmentedControl and implemented custom drawing. 为此,我创建了一个名为SegmentedControlUIControl子类,并实现了自定义绘图。 However, with such a view, I need the option to set what the available options are. 但是,有了这样的视图,我需要选项来设置可用的选项。 I could have just accessed the view from the controller's viewDidLoad() and set those there, but I like using the interface builder for that kind of stuff. 我本来可以从控制器的viewDidLoad()访问该视图并在那里设置那些视图,但我喜欢使用界面构建器来处理那种东西。

So I discovered this wonderful thing called "User Defined Runtime Attributes." 所以我发现了一个名为“用户定义的运行时属性”的奇妙之处。 I created a String attribute with a key buttonValues and set a value (this is a simple Male/Female chooser so I went with "Male|Female"). 我创建了一个带有键buttonValuesString属性并设置了一个值(这是一个简单的男/女选择器,所以我选择了“男|女”)。 I found out that you can access these values using the function self.valueForKey() and pass in the key. 我发现你可以使用self.valueForKey()函数访问这些值并传入密钥。 I made a parser to turn that string into an array and then added functionality for the drawRect() function to use the array to set up the buttons. 我创建了一个解析器将该字符串转换为数组,然后为drawRect()函数添加了功能,以使用该数组来设置按钮。

When I ran the app, I got an error about "Key Value Coding-compliance." 当我运行该应用程序时,我收到有关“键值编码兼容性”的错误。

So I looked that up, and I found out that the class has to have backing variables to store the attributes. 所以我查了一下,发现该类必须有支持变量来存储属性。 So fine, I added an instance variable called buttonValues and initialized it to "" . 很好,我添加了一个名为buttonValues的实例变量,并将其初始化为"" Now the app runs fine but the value comes out empty from the self.valueForKey() function. 现在app运行正常但是self.valueForKey()函数的值是空的。 I looked up tutorials on how to set up user defined runtime attributes but they don't go into enough detail. 我查阅了有关如何设置用户定义的运行时属性的教程,但它们没有详细说明。 They talk about Key Value Coding-compliance like it's something I should just know. 他们谈论Key Value Coding-compliance,就像我应该知道的那样。

I would like to know exactly what I must do for this to work properly, in gory detail . 我想知道我必须做些什么才能使这个工作正常, 详细的

For your purposes you can use either user-defined runtime attributes or expose your class and properties as editable in Interface Builder. 出于您的目的,您可以使用用户定义的运行时属性,或者在Interface Builder中将您的类和属性公开为可编辑。 Either way, you'll want to declare your properties as implicitly unwrapped Optional variables -- IBOutlets are created the same way. 无论哪种方式,您都希望将属性声明为隐式展开的可选变量 - IBOutlets以相同的方式创建。 If you need to make other changes once your properties have a value, give them didSet property observers. 如果您需要在属性具有值时进行其他更改,请为它们didSet属性观察者。 The properties will be at their default value during initialization (or nil if no default was set) and set when the view is added to a superview. 初始化期间属性将为其默认值(如果未设置默认值,则为nil ),并在将视图添加到超级视图时进行设置。

User Defined Runtime Attributes 用户定义的运行时属性

This works more or less like you've described above -- here's the simplest version: 这或多或少像你上面描述的那样 - 这是最简单的版本:

class LabeledView : UIView {
    var viewLabel: String! {
    didSet {
        println("didSet viewLabel, viewLabel = \(self.viewLabel)")
    }
    }

    init(coder aDecoder: NSCoder!)  {
        super.init(coder: aDecoder)
        println("init with coder, viewLabel = \(self.viewLabel)")
    }
}

Then set the viewLabel attribute to "Hello" on the view in your storyboard, like so: 然后在viewLabel的视图viewLabel属性设置为“Hello”,如下所示:

Xcode面板显示用户定义的运行时属性

When you build & run, the console will show that the property is being set correctly: 在构建和运行时,控制台将显示正确设置了属性:

init with coder, viewLabel = nil
didSet viewLabel, viewLabel = Hello

@IBDesignable & @IBInspectable @IBDesignable和@IBInspectable

This gives your custom view a much nicer interface in IB -- set the @IBDesignable attribute on your class and @IBInspectable attributes on each property. 这使您的自定义查看IB一个更漂亮的界面-设置@IBDesignable属性的类和@IBInspectable每个属性的属性。 Here's the same class: 这是同一个班级:

@IBDesignable class LabeledView : UIView {
    @IBInspectable var viewLabel: String!

    init(coder aDecoder: NSCoder!)  {
        super.init(coder: aDecoder)
    }
}

Now you can set your inspectable properties at the top of the Attributes Inspector in Interface Builder: 现在,您可以在Interface Builder中的Attributes Inspector顶部设置可检查属性:

具有可检查属性的Inspector属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM