在 iPhone 中的 UILabel 中显示文本

发布于 2024-10-09 04:08:25 字数 408 浏览 4 评论 0原文

好的。我的代码有什么问题吗?


- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挖个坑埋了你 2024-10-16 04:08:26

如果您已经建立了从 IBOutlet 到 Interface Builder 的所有正确连接,它们将自动为您初始化。您根本不应该重新初始化该对象。 (顺便说一句,您没有完全初始化它)。

因此,去掉 -viewDidLoad 中的初始化代码,它应该可以工作。

- (void)viewWillAppear:(BOOL)animated {
  lblResult.text = @"BlahBlah";
}

If you have already made all of the correct connections from your IBOutlets 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.

- (void)viewWillAppear:(BOOL)animated {
  lblResult.text = @"BlahBlah";
}
糖粟与秋泊 2024-10-16 04:08:26

尝试将框架添加到标签

示例 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";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文