为什么 UITabBarController 中的 viewDidAppear 在视图出现之前执行?

发布于 2024-10-05 00:47:40 字数 748 浏览 0 评论 0原文

我有一个 UITabBarController ,它嵌套一个 UIView 子类(ImageViewer)作为它的第三个选项卡。

在这个 ImageViewer 子类中,我调用了 viewDidAppear 方法:

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

    /* ... start custom code ... 
    NSLog(@"viewDidAppear tag 1 passed);          /* BREAKPOINT 1 here

    [myUIActivityIndicator stopAnimating];

    NSLog(@"viewDidAppear tag 2 passed);          /* BREAKPOINT 2 here
    /* ... end custom code ... 
}

该方法是自动调用的,但奇怪的是,视图只有在该方法完全处理后才出现?

当我按照指示设置断点(1 和 2)时,处理(选择选项卡后)会停止,而上一个选项卡仍然显示。只有在第二个断点后单击继续时,才会显示该视图。 (仅供参考,NSLog 会立即执行)。

在这种情况下, viewDidAppear 的行为更像 viewWillAppear ....

有任何线索可能会发生什么吗?

干杯

I have a UITabBarController that nests a UIView-Subclass (ImageViewer) as it's third tab.

In this ImageViewer Subclass I call the viewDidAppear method:

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

    /* ... start custom code ... 
    NSLog(@"viewDidAppear tag 1 passed);          /* BREAKPOINT 1 here

    [myUIActivityIndicator stopAnimating];

    NSLog(@"viewDidAppear tag 2 passed);          /* BREAKPOINT 2 here
    /* ... end custom code ... 
}

the method is called automatically, but strangely the view only appears after this method has been processed completely?

When I set breakpoints (1 and 2) as indicated, the processing (upon selecting the tab) stops whilst the previous tab is still showing. Only when clicking continue after the second breakpoint, the view will be displayed. (FYI the NSLogs are carried out immeldiately).

In this case viewDidAppear behaves more like viewWillAppear ....

Any clues what might be going on?

Cheers

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

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

发布评论

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

评论(2

菊凝晚露 2024-10-12 00:47:40

如果您希望在视图加载时允许重新绘制屏幕,​​但要触发 -viewDidAppear: 中的其他更新代码,请使用 performSelector:withObject:afterDelay:像这样:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self performSelector:@selector(updateUI) withObject:nil afterDelay:0.0];
}

…

- (void)updateUI
{
    // Do your UI stuff here
}

当你这样做时,当前的事件循环将很快完成,并且 UIKit 将能够在视图加载后重新绘制屏幕。 updateUI 将在下一个事件循环中调用。如果您必须在加载视图后执行计算密集型计算或更新,那么这是获得快速视图转换的好方法。

If you want to allow the screen to be re-drawn when your view loads, but to trigger some other updating code in -viewDidAppear:, use performSelector:withObject:afterDelay: like this:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self performSelector:@selector(updateUI) withObject:nil afterDelay:0.0];
}

…

- (void)updateUI
{
    // Do your UI stuff here
}

When you do it this way, the current event loop will finish quickly, and UIKit will be able to re-draw the screen after your view has loaded. updateUI will be called in the next event loop. This is a good way to get snappy view transitions if you have to perform computationally intensive calculations or updates after a view has loaded.

夏九 2024-10-12 00:47:40

从声音来看,如果您主动调用该方法,设备在 viewDidAppear 方法中运行“自定义代码”时可能没有时间实际显示视图。在这种情况下,您应该让程序调用 viewDidAppear 方法本身。

您的程序也可能正在处理其他代码,这会减慢视图的显示速度,这可以使用计时器来解决。即而不是:

[self otherCode];

你会写:

[NSTimer scheduledTimerWithTimeInterval:.5 
    target:self 
    selector:@selector(otherCode) 
    userInfo:nil 
    repeats:NO];

你可能想尝试以这种方式简单地用计时器延迟你的“自定义代码”。

From the sound of it, if you are actively calling the method, the device might not have time to actually display the view while it is running the "custom code" in your viewDidAppear method. I that case you should let the program call the viewDidAppear method itself.

Your program may also be working on other code which would slow down the appearance of the view, this can be solved using timers. i.e. instead of:

[self otherCode];

you would write:

[NSTimer scheduledTimerWithTimeInterval:.5 
    target:self 
    selector:@selector(otherCode) 
    userInfo:nil 
    repeats:NO];

you might want to try simply delaying your "custom code" with a timer in this way.

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