我有一个名为 TimeTooltipView 的自定义 View 。这是代码:
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
我在 View Controller 中添加了一个 TimeTooltipView,如下所示:
TimeTooltipView *timeTooltipView = [[TimeTooltipView alloc]
initWithFrame: CGRectMake(100, 100, 50, 50)];
timeTooltipView.timeLabel.text = @"TEST";
[self.view addSubview:timeTooltipView];
timeTooltipView.timeLabel.text = @"TEST2";
我需要从 View Controller 即时更改 timeLabel 的文本。使用上面的代码, View 被添加并具有绿色背景色。但标签文本永远不会更改为“TEST”或“TEST2”。
如何即时更改自定义 View 的标签文本?
最佳答案
-(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];
}
关于ios - 即时更改自定义 UIView 中的标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41389708/