ios - Segue 后 UILabel 不更新

标签 ios objective-c xcode uilabel

我转到 UIViewController,在其初始化方法期间,应该指定 2 个标签的文本。然而,这并没有发生,文本永远不会改变。所有的连接都是正确的,但是他们什么都不显示,我不明白为什么。

下面是应该更改此特定 ViewController 的 UILabel 的代码。

-(id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        _nameLabel.text = @"NAME";
        _addressLabel.text = @"ADDRESS"; 
    }
    return self;
}

下面是它的“.h”文件:

@property (strong, nonatomic) Location *selectedLocation;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *addressLabel;

有什么想法吗?

最佳答案

我想 initWithCoder: 没有被调用。 建议在viewDidLoad中设置你想要的数据。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.nameLabel.text = @"NAME";
    self.addressLabel.text = @"ADDRESS"; 
}

如果未解决,请检查 View 状态,例如框架、隐藏或 alpha 值。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"[nameLabel] frame : %@, hidden : %d, alpha : %@", NSStringFromRect(self.nameLabel.frame), self.nameLabel.hidden, self.nameLabel.alpha);
    NSLog(@"[addressLabel] frame : %@, hidden : %d, alpha : %@", NSStringFromRect(self.addressLabel.frame), self.addressLabel.hidden, self.addressLabel.alpha);
}

更新: 这是因为 IBOutlet 在函数 initWithCoder: 中是 nil

XIB-instantiated Object's IBOutlet is nil

Why isn't initWithCoder initializing items correctly?

关于ios - Segue 后 UILabel 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30180616/

相关文章:

ios - block 如何处理 __weak 引用

ios - 自动布局(约束)与自动调整大小掩码(Springs & Struts)

xcode - 如何知道何时将文本粘贴到 UITextView 中

ios - Apple AppStore 如何对搜索结果进行排名?

iphone - 通过monotouch将iPhone应用程序部署到单个电话的最低成本是多少?

iphone - Objective-C UILabel 作为超链接

xcode - 如何分析 Swift 框架?

ios - UIScrollView初始偏移量

ios - 分析 iOS 应用程序电量使用情况

xcode - 如何强制将新的 CNContact 放入本地 CNContainer?