Objective-C에서 IBInspectable의 주소를 설정하는 방법은 무엇입니까?
IBInspectable-properties의 관계를 다음과 같이 알고 있습니다.
@IBInspectable var propertyName:propertyType = defaultValue
Swift에서. 그러나 Objective Objective에서 어떤 효과를 얻으려면 Interface Builder에서 일부 속성의 배열을 복잡하게 할 수 있습니까?
때문에 설정 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
나는 이렇게 내 코드를 썼다. 인터페이스 빌더에서 디자인하거나 앱으로 사용할 때 모두 잘 작동합니다.
@interface MyView : UIView
@property (copy, nonatomic) IBInspectable propertyType *propertyName;
@end
- (void)makeDefaultValues {
_propertyName = defaultValue;
//Other properties...
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self makeDefaultValues];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self makeDefaultValues];
}
return self;
}
나는 그렇게 사용하고있다
@IBInspectable var propertyNameValue:propertyType?
var propertyName:propertyType { return propertyNameValue ?? defaultValue }
경우 propertyNameValue
가 nil
, propertyName
돌아갑니다 defaultValue
.
다음과 같은 매크로를 사용하지 않는 이유는 무엇입니까?
#if TARGET_INTERFACE_BUILDER
// .....IB only specific code when being rendered in IB
#endif
???
prepareForInterfaceBuilder
선택 또한 IB의 특정 코드를 구현하는 데 도움이 될 수 있습니다.
이 두 가지 사항에 대한 자세한 내용은 https://developer.apple.com/library/ios/recipes/xcode_help-IB_objects_media/chapters/CreatingaLiveViewofaCustomObject.html을 참조 하십시오.
먼저 getter를 재정의하려고 시도하고 다음과 같이했습니다.
- (UIColor *)borderColor {
return _borderColor ?: [UIColor greenColor];
}
그러나이 경우 선언되지 않은 식별자에 대한 문제를 받았습니다 _borderColor
.
좋아, 사용자 정의 getter를 통해이 문제를 피하려고했습니다.
- (UIColor *)getBorderColor {
return _borderColor ?: [UIColor greenColor];
}
실제로이 메서드를 getter로 지정하지 않기 때문에 적절한 getter가 아닙니다. 선언되지 않은 식별자에 대한 문제가 발생한다고 지적하면 그렇지 않습니다.
그런 다음이 메서드를 사용하여 updateUI 메서드에서 속성 값을 가져옵니다.
또한 setter를 재정의해야합니다.
- (void)setBorderColor:(UIColor *)borderColor {
_borderColor = borderColor;
[self updateUI];
}
'ProgramingTip' 카테고리의 다른 글
.NET 2.0 SDK 프로그램-각 도구의 기능은 무엇입니까? (0) | 2020.11.25 |
---|---|
PHP 변수 보간과 연결 (0) | 2020.11.25 |
git은 zip 파일을 디렉토리로, zip 내부의 파일을 blob으로 처리 할 수 있 있습니까? (0) | 2020.11.25 |
C 표준 라이브러리와 C POSIX 라이브러리의 차이점 (0) | 2020.11.25 |
Java에 SoftHashMap이 있습니까? (0) | 2020.11.25 |