Can*_*ğlu 63 xcode objective-c interface-builder swift ibinspectable
我知道IBInspectable-properties的默认值可以设置为:
@IBInspectable var propertyName:propertyType = defaultValue在斯威夫特.但是我如何在Objective-C中实现类似的效果,以便我可以在Interface Builder中将某些属性的默认值设置为某些属性?
rin*_*aro 56
由于IBInspectable值是在initWithCoder:之前和之后awakeFromNib:设置的,因此可以在initWithCoder:方法中设置默认值.
@interface MyView : UIView
@property (copy, nonatomic) IBInspectable NSString *myProp;
@property (assign, nonatomic) BOOL createdFromIB;
@end
@implementation MyView
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self != nil) {
self.myProp = @"foo";
self.createdFromIB = YES;
}
return self;
}
- (void)awakeFromNib {
if (self.createdFromIB) {
//add anything required in IB-define way
}
NSLog(@"%@", self.myProp);
}
@end
Run Code Online (Sandbox Code Playgroud)