什么是用户定义的运行时属性中的关键路径? [英] What is key Path in user defined runtime attributes?

查看:205
本文介绍了什么是用户定义的运行时属性中的关键路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个项目,我对某个键是什么感到困惑。
我的问题是,什么是 styleName 键路径?它是视图的属性吗?如何找出可用的关键路径?

I have inherited a project and i'm confused on what a certain key is. My question is, what is the styleName key Path ? is it a property to the view ? How can i find out what key Paths are available ?

例如,在我从故事板中选择UILabel后,我检查身份检查器和用户定义的运行时属性i请参阅以下内容:

For example, after i select a UILabel from the storyboard i check the identity inspector and in the user defined runtime attributes i see the following:

我试过打开main-styles.plist文件但不确定它是如何链接在一起的。

I have tried opening the main-styles.plist file but not sure how its linked together.

当我点击属性检查器时(同时仍然保持故事板中的UILabel突出显示)这就是它的样子:

when i click on the attribute inspector (while still keeping the UILabel in the storyboard highlighted) this is what it looks like:

推荐答案

NSKeyValueCoding 协议 UIKit 中的许多对象符合。

There is an NSKeyValueCoding protocol, which many of the objects within UIKit conform to.

NSKeyValueCoding 中的一种方法是 valueForKey:(和许多其他相关方法,检查我链接的文档。)

One of the methods within NSKeyValueCoding is valueForKey: (and many other relevant methods, check the documentation I linked).

通过在一个对象上调用 valueForKey:,我们可以,在运行时,访问在界面构建器上设置的属性。

By calling valueForKey: on an object, we can, at run time, access properties that were set on the interface builder.

因此,例如,在此标签上,我可能会这样做:

So, for example, on this label, I might do something like this:

Objective-C:

NSString *style = [myLabel valueForKey:@"styleName"];

Swift:

let style = myLabel.valueForKey("styleName")



<现在我可以通过Interface Builder获取设置值,在运行时,我可以根据此处设置的值对标签执行某些操作。例如,在这里,我可能会使用特定的样式名称以特定方式设计标签。

Now I can grab the value set through the Interface Builder and at run time, I can do something with the label based on what value was set here. For example, here, I might use the particular "style name" to design the label in a particular way.

如果您在项目中搜索 valueForKey styleName,您可能会发现此属性的使用位置以及正在使用的内容。

If you search the project for valueForKey or "styleName", you will likely find where this property is being used and what's being done with it exactly.

要跟进我关于属性检查器的问题,从Xcode 6开始,我们可以使用 @IBInspectable 属性,用于创建将显示在Attributes Inspector中的属性(如此处所见)。考虑这个 UIView 扩展名:

To follow up about my question regarding the Attribute Inspector, as of Xcode 6, we can use the @IBInspectable property to create properties which will show up in the Attributes Inspector (as seen here). Consider this UIView extension:

extension UIView {
    @IBInspectable var borderColor : UIColor? {
        set (newValue) {
            self.layer.borderColor = (newValue ?? UIColor.clearColor()).CGColor
        }
        get {
            return UIColor(CGColor: self.layer.borderColor)
        }
    }
}

现在,如果我们在故事板中查看属性检查器中的任何 UIView (或子类),我们将看到:

Now if we take a look at the Attributes inspector for any UIView (or subclass) in our storyboard, we'll see this:

我们现在通过属性检查器提供了边框颜色属性,该属性通常不存在。我指出这个工具的原因是因为每当你通过Attributes Inspector设置其中一个属性时,你设置的值实际上存储为这些用户定义的运行时属性之一:

We now have a "Border Color" property available via the Attributes Inspector which isn't ordinarily there. The reason I point this tool out is because whenever you set one of these properties via the Attributes Inspector, the value you set is actually stored as one of these "User Defined Runtime Attributes":

和每当从我的应用程序中的XIB加载此视图时,首先发生的事情之一就是我的 borderColor 属性将被设置为我选择的这种红色Interface Builder。

And whenever this view is loaded from the XIB in my app, one of the first things that will happen is that my borderColor property will be set to this red color I've selected in the Interface Builder.

这篇关于什么是用户定义的运行时属性中的关键路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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