UILabel第一次不会更新

发布于 2024-10-08 12:38:43 字数 751 浏览 0 评论 0原文


我有一个模态视图,其中仅包含 o 按钮和 uilabel。
按钮关闭模态 v:

- (IBAction) close {
    [self dismissModalViewControllerAnimated:YES];
}


现在,我创建了这个视图:

nw = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:nil];
nw.modalInPopover = YES;
nw.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

[nw setValues:@"asdasd"];

[self presentModalViewController:nw animated:YES];

一切正常。

它加载正确,但 uilabel 值没有显示正确的文本标签。

怎么可能?
第二次单击,值设置正确...

这是setValues func:

- (void) setValues:(NSString*)val {
    [firstLabel setText:val];
}

调试,它第一次显示UILabel 0x0...这是一个问题,但第二次它正确创建并且值设置。

有什么办法解决吗?

阿尔贝托.

i have a modal view which contains only o button and a uilabel.
The button close the modal v:

- (IBAction) close {
    [self dismissModalViewControllerAnimated:YES];
}

Now, i create this view:

nw = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:nil];
nw.modalInPopover = YES;
nw.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

[nw setValues:@"asdasd"];

[self presentModalViewController:nw animated:YES];

all ok.

It loads correctly but the uilabel value don't show me the correct text label.

How it's possible?
Second time click, the value was setted correctly...

This is setValues func:

- (void) setValues:(NSString*)val {
    [firstLabel setText:val];
}

Debugging, it show first time UILabel 0x0... this is a problem, but second time it's created correctly and the value is setted.

any idea to fix?

alberto.

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

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

发布评论

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

评论(3

甜妞爱困 2024-10-15 12:38:43

可能您在 viewDidLoad 中设置了标签文本,直到视图完成自身加载。

尝试放入 viewDidAppearviewWillAppear 代替。

Probably you set label text in viewDidLoad, until view had finished loading itself.

Try put in viewDidAppear or viewWillAppear instead.

┊风居住的梦幻卍 2024-10-15 12:38:43

第一次调用 setValues 时,NewsViewController 可能尚未加载其视图。要进行确认,请在 [ns setValues:@"asdasd"]; 之前和 NewsViewController-[viewDidLoad] 方法中设置断点。

要解决此问题,您可以为 NewsViewController 中的文本添加一个实例变量。然后在 -[setValues:] 中设置实例变量,并在该实例变量的 -[viewDidLoad] 方法中设置标签的文本。

On your first call to setValues, NewsViewController probably hasn't yet loaded its view. To confirm, set a breakpoint before [ns setValues:@"asdasd"]; and in the -[viewDidLoad] method of NewsViewController.

To work around this, you could add an instance variable for the text in NewsViewController. Then set the instance variable in -[setValues:] and set the label's text in its -[viewDidLoad] method from that instance variable.

请你别敷衍 2024-10-15 12:38:43

您还可以在主线程上设置文本

 dispatch_async(dispatch_get_main_queue(), ^{
       firstLabel.text = currentEventsString;
 });

You could also set text on the main thread

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