2016-12-30 85 views
0

我有一個名爲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」。

如何隨時更改自定義視圖的標籤文本?

回答

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