presentModalViewController耗时的操作

时间:2012-06-01 08:58:52

标签: iphone ios performance

我有一个UINavigationController,在UINavigationBar上我有一个Button。当我按下这个按钮时,一个新的UINavigationController以模态方式呈现为UIModalPresentationFormSheet。这很有效。

但我认识到FormSheet的呈现非常耗时。我在动作方法中放了两个日志(“开始”和“完成”,见下面的代码),一个在第一行,一个在最后一行。日志时间告诉我,运行此代码大约需要1.5到2秒。

这是从UINavigationItem调用的Action方法:

- (IBAction)addBtnPressed:(id)sender{
  NSLog(@"start");

  FooViewController *fooContr = [[FooViewController alloc] init];
  fooContr.delegate = self;
  UINavigationController *navContr = [[UINavigationController alloc] initWithRootViewController:fooContr];
  [fooContr release];
  navContr.modalPresentationStyle = UIModalPresentationFormSheet;

  [self.navigationController presentModalViewController:navContr animated:YES];

  [navContr release];

  NSLog(@"finish");

}

似乎就行了

 [self.navigationController presentModalViewController:navContr animated:YES];

需要90%的时间。

有谁知道这里发生了什么以及如何优化它?

3 个答案:

答案 0 :(得分:2)

使用UINavigationController预加载FooViewController,因此当您展示它时,它已经在内存中。

加载FooViewController实例的视图可能需要花费很多时间。

您可以通过对其执行某些操作来加载它。喜欢

UIView *view = fooContr.view;

您应该在显示模式视图控制器的按钮被单击之前执行此操作。否则它可能会产生同样的效果。

答案 1 :(得分:0)

性能问题必须是因为您的navContrManAufn需要时间来创建自己。它在init上执行了什么?

答案 2 :(得分:0)

尽量减少viewControllers中init中的代码,尽可能将代码移动到viewDidLoad,或者甚至将viewDidApped移动到适当的位置(代码意味着只运行一次应该在viewDidload中)。

相关问题