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

标签 ios objective-c

我有一个名为 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/

相关文章:

ios - 与7和8兼容的iOS应用

ios - 其他 View 大小在自动布局中自动更改为零

objective-c - 无法从 Objective-C++ 代码调用 swift 函数

ios - 使 requestAVAssetForVideo 同步

objective-c - 数学函数未显示正确结果

ios - 如何以编程方式在 UINavigationController 中显示第二个模态 ViewController

ios - fillMode 为 kCAFillModeBackwards 有什么影响?

ios - 组合多个MTKView/CAMetalLayer显示

ios - 具有后台刷新功能的 APN。刷新调用两次,何时传递 APN 以及何时点击通知?

iOS 照片框架 : how to revert image asset to original