从 xib 加载子类时如何设置子类 UIView 属性的值
How to set values of subclassed UIView properties when loading subclass from a xib
我有一个名为 BannerHeaderView 的子classed UIView,并按照我希望的方式设计它在 xib 中的布局。我已将 xib class 设置为 BannerHeaderView。我在 IB 中连接了一个 UILabel 和一个 UIImageView。
问题是,当我在 ViewController 中初始化 BannerHeaderView 后去设置这些子视图的值时,标签和图像视图为空。我也试过在 init 中设置它们。
BannerHeaderView.h的代码:
@interface BannerHeaderView : UIView
@property (strong, nonatomic) IBOutlet UIImageView *bannerImage;
@property (strong, nonatomic) IBOutlet UILabel *headerLabel;
-(void)setTitle:(NSString *)title;
-(void)setImageFile:(PFFile *)imageFile;
@end
BannerHeaderView.m的代码:
@implementation BannerHeaderView
@synthesize headerLabel, bannerImage;
-(id)init{
self = [super init];
if (self) {
[self customInit];
}
return self;
}
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self customInit];
}
return self;
}
-(void)customInit{
UIView *contentView = [[[NSBundle mainBundle] loadNibNamed:@"BannerHeaderView"
owner:self
options:nil] objectAtIndex:0];
[self addSubview:contentView];
contentView.frame = self.bounds;
}
-(void)setTitle:(NSString *)title{
NSLog(@"HEADER = %@", headerLabel);
headerLabel.text = title;
}
我正在 ViewController 中创建 BannerHeaderImage,如下所示:
bannerHeader = [[BannerHeaderView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, bannerHeight + segmentedHeaderHeight)];
[bannerHeader setTitle:@"I AM THE NEW TITLE"];
日志告诉我 headerLabel 为空。
问题:
如何设置这些子视图的值?
我应该以编程方式设计布局吗?
感谢您的宝贵时间。
P.S。我花了几个小时研究这个,但找不到解决方案。我确实在 SO 上找到了类似的问题,但是 none 有合适的答案使我能够解决我的问题...
请像这样将 class BannerHeaderView 设置为文件所有者
我有一个名为 BannerHeaderView 的子classed UIView,并按照我希望的方式设计它在 xib 中的布局。我已将 xib class 设置为 BannerHeaderView。我在 IB 中连接了一个 UILabel 和一个 UIImageView。
问题是,当我在 ViewController 中初始化 BannerHeaderView 后去设置这些子视图的值时,标签和图像视图为空。我也试过在 init 中设置它们。
BannerHeaderView.h的代码:
@interface BannerHeaderView : UIView
@property (strong, nonatomic) IBOutlet UIImageView *bannerImage;
@property (strong, nonatomic) IBOutlet UILabel *headerLabel;
-(void)setTitle:(NSString *)title;
-(void)setImageFile:(PFFile *)imageFile;
@end
BannerHeaderView.m的代码:
@implementation BannerHeaderView
@synthesize headerLabel, bannerImage;
-(id)init{
self = [super init];
if (self) {
[self customInit];
}
return self;
}
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self customInit];
}
return self;
}
-(void)customInit{
UIView *contentView = [[[NSBundle mainBundle] loadNibNamed:@"BannerHeaderView"
owner:self
options:nil] objectAtIndex:0];
[self addSubview:contentView];
contentView.frame = self.bounds;
}
-(void)setTitle:(NSString *)title{
NSLog(@"HEADER = %@", headerLabel);
headerLabel.text = title;
}
我正在 ViewController 中创建 BannerHeaderImage,如下所示:
bannerHeader = [[BannerHeaderView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, bannerHeight + segmentedHeaderHeight)];
[bannerHeader setTitle:@"I AM THE NEW TITLE"];
日志告诉我 headerLabel 为空。
问题:
如何设置这些子视图的值? 我应该以编程方式设计布局吗?
感谢您的宝贵时间。
P.S。我花了几个小时研究这个,但找不到解决方案。我确实在 SO 上找到了类似的问题,但是 none 有合适的答案使我能够解决我的问题...
请像这样将 class BannerHeaderView 设置为文件所有者