What is key Path in user defined runtime attributes?

怎甘沉沦 提交于 2019-12-02 23:30:25
nhgrif

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

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

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")

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.

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.


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)
        }
    }
}

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

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":

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.

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

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 exist in 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.

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. 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:

- (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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!