2016-12-30 5 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]; 
} 
Смежные вопросы