我有一个UIView的子类,我从NIB文件加载。我的NIB文件包含一个UIButton和一个IBOutlet,名为 clock
@property (strong, nonatomic) IBOutlet UIButton *clock;
我的界面构建器上的时钟按钮中设置的文本显示出来!但是我从initWithCoder添加的另一个子视图并不是。这是为什么?
.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文件
@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
谢谢!
答案 0 :(得分:2)
这是我要做的:
如果您不需要使用框架,请删除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];
}
致电[TWTimerView loadViewFromNIBFile]
以获取您的观看实例。
答案 1 :(得分:0)
你的TWTimerView有一个笔尖,你有一个UIButton吗?你有一个UIView-Subclass,它为你的UIButton添加了三个UIImageViews,为什么你这样做?!
为什么不对UIButton进行子类化并在那里设置UIImageView?
根本我会改变它:
@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