为什么viewDidLoad在更新UI之前会等待? [英] Why does viewDidLoad wait before updating the UI?

查看:81
本文介绍了为什么viewDidLoad在更新UI之前会等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是试图了解iPhone上的Objective-C事件模型,以及我从根本上误解了一些事情的外观。

I'm just trying to get my head around the Objective-C event model on iPhone, and by the looks of things I have fundamentally misunderstood something here.

出于实验的目的,在视图控制器的-viewDidLoad方法中,我设置了UILabel的文本,然后睡了两秒钟,然后再次更改标签的文本。

For the purpose of experimentation, in a view controller's -viewDidLoad method, I am setting a UILabel's text, then sleeping for two seconds, and then changing the label's text again.

我的期望如下:标签首先读取First Text,然后两秒钟后它将更新为Second Text。当然,这并不是这样的。相反,视图在所有中不可见两秒钟,最后当它变为可见时,其标签显示为Second Text。

My expectations are as follows: the label will first read, "First Text", then two seconds later it will be updated to read, "Second Text." Of course, this isn't quite how it happens. Instead, the view isn't visible at all for two seconds, and finally when it becomes visible its label reads, "Second Text."

有人可以向我解释发生了什么事吗?我很想知道你们将如何实现我的目标。

Could somebody please explain to me what is going on? I'm interested to find out how you guys would achieve what I'm going for here.

干杯。

UPDATE 1 :这是viewDidLoad方法:

UPDATE 1: Here's the viewDidLoad method:

- (void)viewDidLoad {
    [super viewDidLoad];
    label.text = @"First Label";
    sleep(2);
    label.text = @"Second Label";
}

更新2 :我在这里犯了一个愚蠢的错误,请忽略此更新。

UPDATE 2: I made a silly mistake here, so please ignore this update.

更新3 :我现在已将以下内容添加到我的viewDidAppear方法中:

UPDATE 3: I have now added the following to my viewDidAppear method:

- (void)viewDidAppear: (BOOL)animated {
    [super viewDidAppear: animated];
    label.text = @"First Label";
    sleep(2);
    label.text = @"Second Label";
}

不幸的是我遇到了完全相同的问题。

Unfortunately I'm having exactly the same problem.

更新4 :根据gerry3和Felix的建议,我现在已经实现了performSelector,并且热潮!有效!我将不得不把它交给gerry3,因为他肯定付出了最大的努力来帮助我。感谢您的所有贡献!

UPDATE 4: Following gerry3 and Felix's suggestions, I have now implemented a performSelector, and boom! Works a treat!! I'm going to have to give it to gerry3 though as he certainly put the most amount of effort into helping me out. Thanks for all your contributions!

推荐答案

将您的代码修改为 viewDidAppear:

viewDidLoad viewWillAppear时,视图尚未显示: 被调用。

更新

为了清楚起见,我同意其他人的看法正确的方法是使用延迟的方法调用或计时器。

UPDATE
Just to be clear, I agree with the others that the correct way to do this is with a delayed method call or timer.

UPDATE 2

这是我建议的代码:

UPDATE 2
Here is the code that I am suggesting:

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    NSLog(@"View will appear!");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSLog(@"View did appear!");

    label.text = @"First Label";
    sleep(2);
    label.text = @"Second Label";
}

以及正确方式:

- (void)viewDidLoad {
    [super viewDidLoad];

    label.text = @"First Label";
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    NSLog(@"View will appear!");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSLog(@"View did appear!");

    [self performSelector:@selector(changeLabelText) withObject:nil afterDelay:2.0f];
}

- (void)changeLabelText {
    label.text = @"Second Label";
}

这篇关于为什么viewDidLoad在更新UI之前会等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆