自定义UIVIew的UI_APPEARANCE_SELECTOR

时间:2014-06-12 08:22:34

标签: ios objective-c uiview

我尝试将UI_APPEARANCE_SELECTOR应用于我的自定义类

MyCustomView.h

@interface MyCustomView : UIView
@property (nonatomic, strong) UIImage *indicatorImage UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *myColor UI_APPEARANCE_SELECTOR;

- (void) doSomething;
@end

MyCustomView.m

@interface MyCustomView()
@property (nonatomic, strong) UIImageView *indicatorImageView;
@end

@implementation MyCustomView

-(void)initialize {
   [self addSubview:self.indicatorImageView];
}

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aCoder
{
    self = [super initWithCoder:aCoder];
    if(self) {
        [self initialize];
    }

    return self;
}

- (void) doSomething {
    // here indicatorImage and myColor are nil!    
}

- (void) setIndicatorImage:(UIImage *)indicatorImage {
    _indicatorImage = indicatorImage;
    self.indicatorImageView.image = indicatorImage;
    [self.indicatorImageView setNeedsLayout];
}

- (UIImageView *)indicatorImageView
{
    if (!_indicatorImageView) {
        _indicatorImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _indicatorImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }
    return _indicatorImageView;
}

- (void)drawRect:(CGRect)rect {
    // here indicatorImage and myColor are NOT nil!
}
@end

我在AppDelegate中设置了我的外观。我将我的观点添加到故事板并分配了MyCustomView课程。在viewDidLoad中,我从上面的代码中调用doSomething方法,并且令人惊讶的是两个变量都是零。另一个奇怪的事情是indicatorImageView包含我在AppDelegate中设置的图像。最后,我将两个变量都设置为drawRect中的正确值。

问题是:我做错了什么?如何在doSomething方法中获得正确的值?

1 个答案:

答案 0 :(得分:2)

这是因为在viewDidLoad方法之后设置了外观。 调用View Controller上的viewDidLoad方法时,视图已加载,但尚未添加到视图层次结构中。

如果在setMyColor:方法中放置断点并打印回溯,您将看到:

  • 在控制器上的setMyColor:之后调用viewDidLoad方法
  • addSubview方法之后应用外观,该方法始终在父控制器上的viewDidLoad方法之后发生。

看第8帧,_applyAppearanceInvocations

frame #1: 0x00d8d18d CoreFoundation`__invoking___ + 29
frame #2: 0x00eadb92 CoreFoundation`-[NSInvocation invokeUsingIMP:] + 242
frame #3: 0x0186364b UIKit`__workaround10030904InvokeWithTarget_block_invoke + 95
frame #4: 0x012be830 UIKit`+[UIView _performCustomizableAppearanceModifications:] + 29
frame #5: 0x018635e1 UIKit`workaround10030904InvokeWithTarget + 1047
frame #6: 0x0185d2f8 UIKit`+[_UIAppearance _applyInvocationsTo:window:matchingSelector:] + 4107
frame #7: 0x0185db14 UIKit`+[_UIAppearance _applyInvocationsTo:window:] + 56
frame #8: 0x012d7b1b UIKit`-[UIView(Internal) _applyAppearanceInvocations] + 287
frame #9: 0x012d85a4 UIKit`__88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 65
frame #10: 0x012d8531 UIKit`-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:] + 204
frame #11: 0x012d845f UIKit`-[UIView(Internal) _didChangeFromIdiom:onScreen:traverseHierarchy:] + 53
frame #12: 0x012d8422 UIKit`-[UIView(Internal) _didChangeFromIdiomOnScreen:traverseHierarchy:] + 172
frame #13: 0x012d79cd UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 1722
frame #14: 0x012d7666 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 851
frame #15: 0x012cea57 UIKit`__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 154
frame #16: 0x012ce9b5 UIKit`-[UIView(Hierarchy) _postMovedFromSuperview:] + 458
frame #17: 0x012da428 UIKit`-[UIView(Internal) _addSubview:positioned:relativeTo:] + 1943
frame #18: 0x012ccdbd UIKit`-[UIView(Hierarchy) addSubview:] + 56