我有一个名为TimeTooltipView的自定义视图。这是代码:
TimeTooltipView.h
#import <UIKit/UIKit.h>
@interface TimeTooltipView : UIView
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
-(void)configureView;
@end
TimeTooltipView.m
#import "TimeTooltipView.h"
@implementation TimeTooltipView
-(id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"TimeTooltipView" owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
[self addSubview:mainView];
[self configureView];
}
return self;
}
-(void)configureView {
self.backgroundColor = [UIColor greenColor];
}
@end
我在视图控制器中添加了一个TimeTooltipView,如下所示:
TimeTooltipView *timeTooltipView = [[TimeTooltipView alloc]
initWithFrame: CGRectMake(100, 100, 50, 50)];
timeTooltipView.timeLabel.text = @"TEST";
[self.view addSubview:timeTooltipView];
timeTooltipView.timeLabel.text = @"TEST2";
我需要从视图控制器动态更改timeLabel的文本。使用上面的代码,视图将被添加并具有绿色背景颜色。但标签文本永远不会改为“TEST”或“TEST2”。
如何动态更改自定义视图的标签文字?
答案 0 :(得分:1)
-(id)initWithFrame:(CGRect)frame {
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"TimeTooltipView" owner:self options:nil];
//Instead of making it subView just replace it.
self = [subviewArray objectAtIndex:0];
self.frame = frame;
[self configureView];
return self;
}
-(void)configureView {
self.backgroundColor = [UIColor greenColor];
}