为什么 UITabBarController 中的 viewDidAppear 在视图出现之前执行?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望在视图加载时允许重新绘制屏幕,但要触发
-viewDidAppear:
中的其他更新代码,请使用performSelector:withObject:afterDelay:
像这样:当你这样做时,当前的事件循环将很快完成,并且 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:
, useperformSelector:withObject:afterDelay:
like this: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.从声音来看,如果您主动调用该方法,设备在 viewDidAppear 方法中运行“自定义代码”时可能没有时间实际显示视图。在这种情况下,您应该让程序调用 viewDidAppear 方法本身。
您的程序也可能正在处理其他代码,这会减慢视图的显示速度,这可以使用计时器来解决。即而不是:
你会写:
你可能想尝试以这种方式简单地用计时器延迟你的“自定义代码”。
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:
you would write:
you might want to try simply delaying your "custom code" with a timer in this way.