viewWillAppear在iOS5中被调用两次 [英] viewWillAppear being called twice in iOS5
问题描述
我正在运行我的所有应用程序以确保它不仅仅是一个应用程序,并且在我拥有的每个应用程序中,当我在iOS5模拟器或设备上运行时, viewWillAppear
方法在每个视图上被调用两次。我有一个简单的 NSLog(@1);
,每次在我的控制台中出现两次。这只是我,还是正在发生的事情? (它只在iOS4中调用一次)
I'm running all my apps to make sure it's not just one app, and in every app I have, when I run on the iOS5 simulator or device, the viewWillAppear
method gets called twice on every view. I have a simple NSLog(@"1");
, and this appears twice in my console every time. Is this just me, or is something going on? (It only gets called once in iOS4)
这是调用viewWillAppear两次调用视图的代码:
This is the code calling the view that calls viewWillAppear twice:
CloseDoorViewController *closeVC;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
closeVC = [[ CloseDoorViewController alloc] initWithNibName:@"CloseDoorViewIpad" bundle:nil];
} else {
closeVC = [[ CloseDoorViewController alloc] initWithNibName:@"CloseDoorViewController" bundle:nil];
}
[self.view addSubview:closeVC.view];
[self presentModalViewController:closeVC animated:NO];
推荐答案
因为您要显示两次视图。
Because you are displaying the view twice.
首次将视图添加为当前视图的子视图:
First time by adding the view as a subview of the current view:
[self.view addSubview:closeVC.view];
第二次将视图的控制器推到当前视图的控制器之上:
Second time by pushing the view's controller on top of current view's controller:
[self presentModalViewController:closeVC animated:NO];
我不知道为什么在iOS4中 viewWillAppear
只被调用一次,因为iOS5正确调用它两次,因为你正如上所述显示视图两次。
I'm not sure why in iOS4 the viewWillAppear
was only called once, because iOS5 is correct to call it twice, given that you are displaying the view twice as explained above.
只需删除其中一行和没关系(我建议删除 addSubview
并保留 presentModalViewController
一个)。
Just remove one of the lines and it would be fine (I'd recommend removing the addSubview
and retain the presentModalViewController
one).
这篇关于viewWillAppear在iOS5中被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!