简体   繁体   English

为什么从自定义UIView向UIButton添加subView不会显示添加的subViews?

[英]Why adding a subView to UIButton, from a custom UIView doesn't show the added subViews?

I have a subclass of UIView, that I'm loading from a NIB file. 我有一个UIView的子类,它是从NIB文件加载的。 My NIB file, contains a UIButton and an IBOutlet to it, called clock 我的NIB文件包含一个UIButton和一个指向时钟的IBOutlet

@property (strong, nonatomic) IBOutlet UIButton *clock;

The text set in my clock button, on my interface builder, shows up! 显示在我的界面生成器上我的时钟按钮中设置的文本! But everother subview that I add from initWithCoder doesn't. 但是我从initWithCoder添加的其他子视图却没有。 Why is this? 为什么是这样?

.h file .h文件

@interface TWTimerView : UIView

@property (strong, nonatomic) IBOutlet UIButton *clock;

@property (strong, nonatomic) UIImageView *circle;
@property (strong, nonatomic) UIImageView *pointer;
@property (strong, nonatomic) UIImageView *tick;

@property (strong, nonatomic) UIImageView *circleGlow;
@property (strong, nonatomic) UIImageView *pointerGlow;
@property (strong, nonatomic) UIImageView *tickGlow;

@end

.m file .m文件

@implementation TWTimerView

-(id)initWithCoder:(NSCoder *)aDecoder {

    self = [super initWithCoder:aDecoder];
    if(self) {

        self.backgroundColor = [UIColor clearColor];

        _circle  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"circle"]];
        _pointer = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pointer"]];
        _tick    = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick"]];

        [_clock addSubview:_circle];
        [_clock addSubview:_pointer];
        [_clock addSubview:_tick];

    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        TWTimerView *view= [[[NSBundle mainBundle] loadNibNamed:@"TimerView" owner:self options:nil] objectAtIndex:0];
        [self addSubview:view];

    }
    return self;
}

@end

Thank you! 谢谢!

Here is what I would do : If you dont need to use the frame, drop the initWithFrame method and use another method to help you load the view, like this. 这就是我要做的事情:如果不需要使用框架,则删除initWithFrame方法,然后使用另一种方法来帮助您加载视图,如下所示。

+ (id)loadViewFromNIBFile {
    NSArray * array = [[NSBundle mainBundle] loadNibNamed:@"TimerView" owner:self options:nil];
    //You may want to assert array only contains one element here
    TWTimerView * view = (TWTimerView *)[array objectAtIndex:0];
    NSAssert([view isKindOfClass:TWTimerView.class], @"Unexpected class");
    [view _setDefaultComponents];
    return view;
}

- (void)_setDefaultComponents {
    self.backgroundColor = [UIColor clearColor];
    _circle  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"circle"]];
    _pointer = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pointer"]];
    _tick    = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick"]];
    [_clock addSubview:_circle];
    [_clock addSubview:_pointer];
    [_clock addSubview:_tick];
}

Call [TWTimerView loadViewFromNIBFile] to get a instance of your view. 调用[TWTimerView loadViewFromNIBFile]获取视图的实例。

Your TWTimerView has a nib, in which you have a UIButton? 您的TWTimerView有一个笔尖,其中有一个UIButton? And you have an UIView-Subclass, that is adding three UIImageViews to your UIButton, why you do this?! 并且您有一个UIView-Subclass,它将三个UIImageViews添加到您的UIButton中,为什么要这样做呢?

Why not subclass the UIButton and set the UIImageViews there? 为什么不继承UIButton并在那里设置UIImageViews?

At all i would change it to this: 我将全部更改为:

@implementation TWTimerView

    - (id)initWithFrame:(CGRect)frame
    {

        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            [[[NSBundle mainBundle] loadNibNamed:@"TimerView" owner:self options:nil] objectAtIndex:0];
            self.backgroundColor = [UIColor clearColor];

            _circle  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"circle"]];
            _pointer = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pointer"]];
            _tick    = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick"]];

            [_clock addSubview:_circle];
            [_clock addSubview:_pointer];
            [_clock addSubview:_tick];

        }
        return self;
    }

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM