2016-12-30 13 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]; 
} 
関連する問題