【发布时间】:2013-03-20 22:01:01
【问题描述】:
假设我们有这个CustomButton 接口:
@interface CustomButton : UIButton
@property (nonatomic, assign) CGFloat minWidth;
@end
每次minWidth 改变时,我们都想重新布局我们的CustomButton。据我所知,我们有两种解决方案:
观察属性值
// In -initWithFrame:
[self addObserver:self forKeyPath:@"minWidth" options:0 context:nil];
// In -observeValueForKeyPath:ofObject:change:context:
[self setNeedsLayout];
覆盖 minWidth 的设置器
// In -setMinWidth:
_minWidth = minWidth; // Side note: it's an ARC project
[self setNeedsLayout];
哪一个是正确的解决方案,为什么?
【问题讨论】:
-
This 可能会有所帮助。
标签: ios objective-c cocoa-touch properties key-value-observing