ios 用户定义的运行时属性中的关键路径是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30089193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is key Path in user defined runtime attributes?
提问by j2emanue
I have inherited a project and i'm confused on what a certain key is. My question is, what is the styleNamekey Path ? is it a property to the view ? How can i find out what key Paths are available ?
我继承了一个项目,我对某个键是什么感到困惑。我的问题是,styleName键 Path是什么?它是视图的属性吗?如何找出可用的关键路径?
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 突出显示)时,它是这样的:
回答by nhgrif
There is an NSKeyValueCoding
protocol, which many of the objects within UIKit
conform to.
有一个NSKeyValueCoding
协议,其中的许多对象都UIKit
符合该协议。
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:
目标-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. For example, here, I might use the particular "style name" to design the label in a particular way.
现在我可以通过 Interface Builder 获取设置的值,并且在运行时,我可以根据此处设置的值对标签执行某些操作。例如,在这里,我可能会使用特定的“样式名称”以特定方式设计标签。
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). Consider this UIView
extension:
为了跟进我关于 Attribute Inspector 的问题,从 Xcode 6 开始,我们可以使用该@IBInspectable
属性来创建将显示在 Attributes Inspector 中的属性(如下所示)。考虑这个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
故事板中任何(或子类)的属性检查器,我们将看到:
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 获得一个“Border Color”属性,它通常不存在。我指出这个工具的原因是因为每当您通过属性检查器设置这些属性之一时,您设置的值实际上存储为这些“用户定义的运行时属性”之一:
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 中选择的这个红色。
回答by Amit Jagesha シ
回答by AechoLiu
Based on the 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:
加载笔尖时,自定义视图将收到此消息:
[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 existin the object otherwise an exception will occur.
重要:用户定义的运行时属性的属性或键路径必须存在于对象中,否则会发生异常。
Because those methods are called when the nib
is loaded. So, those runtime attributes can be obtained inside the -(void)awakeFromNib
.
因为这些方法在nib
加载时被调用。因此,这些运行时属性可以在-(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;
}
回答by j2emanue
thanks nhgrif. Actually thanks to your answer which was great so plus one i found whats happening. They created a global category on UIView. its called UIView+mystyle. there they have a method with the following signature:
谢谢 nhgrif。实际上,多亏了您的回答,这很棒,所以我发现了正在发生的事情。他们在 UIView 上创建了一个全局类别。它叫做 UIView+mystyle。他们有一个具有以下签名的方法:
- (void) setStyleName:(NSString*) styleName
so xcode uses this method without the 'set' and matches it to the runtime key path attribute. in this method they are applying the attribute.
因此 xcode 使用此方法而不使用 'set' 并将其与运行时键路径属性匹配。在这种方法中,他们正在应用该属性。