UILabels 在第一次加载视图时不会更新。

发布于 2024-09-10 02:50:30 字数 578 浏览 9 评论 0原文

我有 UITableview 根据所选行设置另一个视图的 UILabel

在第一次选择任何行时,视图会加载空标签(未设置任何内容),一旦我向后导航,然后当我选择之后的一行时,它就会完美加载。

为什么不在第一个选择上设置标签?

我有以下代码:

NSLog(@"object at index 0 is %@", [tempArray2 objectAtIndex:0]);

self.categoryQuoteViewController.aLabel.text = [tempArray2 objectAtIndex:0];
self.categoryQuoteViewController.bLabel.text = [tempArray2 objectAtIndex:1];

NSLog(@"the first label is %@",self.categoryQuoteViewController.aLabel.text);

第一个 NSLog 打印该值,然后在设置它并打印标签的值后,它是 null

I have UITableviews that set another view's UILabels depending on the row selected.

On the first selection of any row, the view loads with empty labels (nothing set), as soon as I navigate back and then when I choose a row after that it loads perfectly.

Why aren't the labels being set on the first selection?

I have the following code:

NSLog(@"object at index 0 is %@", [tempArray2 objectAtIndex:0]);

self.categoryQuoteViewController.aLabel.text = [tempArray2 objectAtIndex:0];
self.categoryQuoteViewController.bLabel.text = [tempArray2 objectAtIndex:1];

NSLog(@"the first label is %@",self.categoryQuoteViewController.aLabel.text);

The first NSLog prints the value, then straight after setting it and printing the label's value it is null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

诗笺 2024-09-17 02:50:30

问题是 categoryQuoteViewController 的视图是延迟加载的,这意味着在您第一次在屏幕上显示视图之前,不会加载任何标签。因此,当您第一次访问categoryQuoteViewController.aLabel时,它将是nil

您可以通过访问视图控制器上的 view 属性来强制加载视图,如下所示:

/* Force the view to load so we can set the labels */
categoryQuoteViewController.view;

The problem is the view of categoryQuoteViewController is loaded lazily, which means none of your labels are loaded until you first display the view on the screen. So when you first access categoryQuoteViewController.aLabel it will be nil.

You can force the view to load by accessing view property on the view controller like this:

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