即时更改自定义UIView中的标签文本

问题描述:

我有一个名为TimeTooltipView的自定义视图。下面是代码:即时更改自定义UIView中的标签文本

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”。

如何随时更改自定义视图的标签文本?

-(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]; 
}