在 iPhone 中的 UILabel 中显示文本
好的。我的代码有什么问题吗?
- (void)viewDidLoad {
[super viewDidLoad];
lblResult = [UILabel alloc];
}
- (void)viewWillAppear:(BOOL)animated {
lblResult.text = @"BlahBlah";
}
我将 lblResult 很好地链接到了 IB 中的 Label 对象。但标签仅显示默认文本。我的巴拉巴拉在哪里?
当我在 IB 中设置的默认字符串实际上设置为 lblResult 时?
我猜 BlahBlah 字符串没有显示的原因是因为 lblResult.text 被 IB 指定的默认字符串覆盖。只是我的猜测。
谁能帮我解释一下吗?
OK. What's wrong with my code?
- (void)viewDidLoad {
[super viewDidLoad];
lblResult = [UILabel alloc];
}
- (void)viewWillAppear:(BOOL)animated {
lblResult.text = @"BlahBlah";
}
I linked lblResult to Label object in IB well. But the label only shows the default text. Where's my BlahBlah??
And when the default string I set in the IB actually set to lblResult??
The reason that BlahBlah string is not displyed is I guess because lblResult.text is over-written by default string specified from IB. Just my guess.
Can anyone make me clear with that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您已经建立了从
IBOutlet
到 Interface Builder 的所有正确连接,它们将自动为您初始化。您根本不应该重新初始化该对象。 (顺便说一句,您没有完全初始化它)。因此,去掉
-viewDidLoad
中的初始化代码,它应该可以工作。If you have already made all of the correct connections from your
IBOutlet
s to Interface Builder, they will be automatically initialized for you. You shouldn't be re-initializing the object at all. (As a side point, you weren't fully initializing it).So get rid of the initialization code in
-viewDidLoad
and it should work.尝试将框架添加到标签
示例 1
lblResult = [[UILabel alloc] initWithFrame:CGRectMake(75, 10, 180, 20)];
示例2
lblResult = [[UILabel alloc] init];
lblResult.frame = CGRectMake(75, 10, 180, 20);
或添加颜色
lblResult = [[UILabel alloc] initWithFrame:CGRectMake(75, 10, 180, 20)];
lblResult.backgroundColor =[UIColor redColor];
lblResult.textColor = [UIColor 白色颜色];
lblResult.text = @"BlahBlah";
try adding the frame to your label
Example 1
lblResult = [[UILabel alloc] initWithFrame:CGRectMake(75, 10, 180, 20)];
Example 2
lblResult = [[UILabel alloc] init];
lblResult.frame = CGRectMake(75, 10, 180, 20);
or Adding color
lblResult = [[UILabel alloc] initWithFrame:CGRectMake(75, 10, 180, 20)];
lblResult.backgroundColor =[UIColor redColor];
lblResult.textColor = [UIColor whiteColor];
lblResult.text = @"BlahBlah";