简体   繁体   English

什么是用户定义的运行时属性中的关键路径?

[英]What is key Path in user defined runtime attributes?

I have inherited a project and i'm confused on what a certain key is. 我继承了一个项目,我对某个键是什么感到困惑。 My question is, what is the styleName key Path ? 我的问题是, styleName键路径是什么? is it a property to the view ? 它是视图的属性吗? How can i find out what key Paths are available ? 我怎样才能找到可用的关键路径?

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: 例如,在我从故事板中选择UILabel后,我检查身份检查器,并在用户定义的运行时属性中,我看到以下内容:

在此输入图像描述

I have tried opening the main-styles.plist file but not sure how its linked together. 我试过打开main-styles.plist文件但不确定它是如何链接在一起的。

when i click on the attribute inspector (while still keeping the UILabel in the storyboard highlighted) this is what it looks like: 当我点击属性检查器(同时仍然保持故事板中的UILabel突出显示)时,它是这样的:

在此输入图像描述

There is an NSKeyValueCoding protocol , which many of the objects within UIKit conform to. 有一个NSKeyValueCoding协议UIKit许多对象都符合NSKeyValueCoding协议

One of the methods within NSKeyValueCoding is valueForKey: (and many other relevant methods, check the documentation I linked). NSKeyValueCoding一个方法是valueForKey:以及许多其他相关方法,请查看我链接的文档)。

By calling valueForKey: on an object, we can, at run time, access properties that were set on the interface builder. 通过在对象上调用valueForKey:我们可以在运行时访问在接口构建器上设置的属性。

So, for example, on this label, I might do something like this: 所以,例如,在这个标签上,我可能会这样做:

Objective-C: Objective-C的:

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

Swift: 迅速:

let style = myLabel.valueForKey("styleName")

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. 现在我可以通过Interface Builder获取设置值,在运行时,我可以根据此处设置的值对标签执行某些操作。 For example, here, I might use the particular "style name" to design the label in a particular way. 例如,在这里,我可能会使用特定的“样式名称”以特定方式设计标签。

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. 如果您在项目中搜索valueForKey"styleName" ,您可能会发现此属性的使用位置以及正在使用的属性。


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 ). 为了跟进我关于属性检查器的问题,从Xcode 6开始,我们可以使用@IBInspectable属性来创建将在Attributes Inspector中显示的属性( 如此处所示 )。 Consider this UIView extension: 考虑这个UIView扩展:

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

Now if we take a look at the Attributes inspector for any UIView (or subclass) in our storyboard, we'll see this: 现在,如果我们在故事板中查看任何UIView (或子类)的Attributes检查器,我们将看到:

在此输入图像描述

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": 我指出这个工具的原因是因为无论何时通过Attributes Inspector设置其中一个属性,您设置的值实际上存储为这些“用户定义的运行时属性”之一:

在此输入图像描述

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. 每当从我的应用程序中的XIB加载此视图时,首先发生的事情之一就是我的borderColor属性将设置为我在Interface Builder中选择的这种红色。

Below is a list of the available attribute types and the corresponding property type. 下面是可用属性类型和相应属性类型的列表。

 Boolean – BOOL (true/false)
 Number – NSNumber * or any numeric scalar, e.g. NSInteger
 String – NSString 
 Point – CGPoint
 Size – CGSize
 Rect – CGRect
 Range – NSRange
 Color – UIColor 

此处图像显示如何定义用户定义的运行时属性

Based on the Apple doc 基于Apple doc

Use user defined runtime attributes to set an initial value for objects that do not have an interface builder inspector. 使用用户定义的运行时属性为没有接口构建器检查器的对象设置初始值。 For example, if you add the following entries in the identity inspector for a custom view: 例如,如果在身份检查器中为自定义视图添加以下条目:

运行时属性的图像

The custom view will get this message when the nib is loaded: 加载nib时,自定义视图将收到此消息:

[customView setValue:[NSNumber numberWithBoolean:NO] forKeyPath:@"isDataLoaded"];
[customView setValue:@"Hatha" forKeyPath:@"excersize.yoga"];
[customView setValue:nil forKeyPath:@"myData"];

Important: The property or key path for the user defined runtime attribute must exist in the object otherwise an exception will occur. 要点:用户定义的运行时属性的属性或键路径必须存在于对象中,否则将发生异常。

Because those methods are called when the nib is loaded. 因为在加载nib时会调用这些方法。 So, those runtime attributes can be obtained inside the -(void)awakeFromNib . 因此,可以在-(void)awakeFromNib获取这些运行时属性。

For example, 例如,

- (void)awakeFromNib
{
// @property (nonatomic) BOOL isDataLoaded, which is assigned by the above `User Defined Runtime Attributes` picture.
   BOOL isLoaded = self.isDataLoaded; 
}

thanks nhgrif. 谢谢nhgrif。 Actually thanks to your answer which was great so plus one i found whats happening. 实际上多亏了你的答案,这很好,加上一个我发现了什么。 They created a global category on UIView. 他们在UIView上创建了一个全球类别。 its called UIView+mystyle. 它叫做UIView + mystyle。 there they have a method with the following signature: 他们有一个带有以下签名的方法:

- (void) setStyleName:(NSString*) styleName

so xcode uses this method without the 'set' and matches it to the runtime key path attribute. 因此xcode使用此方法而不使用'set'并将其与运行时键路径属性相匹配。 in this method they are applying the attribute. 在这种方法中,他们正在应用该属性。

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

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