我在这里为我正在开发的iOS应用程序编写了一些代码,出于某种原因,每次看起来像一个快速而简单的任务需要我的iPhone 4S完成第二个或更多工作。为什么实例化UIAlertView需要这么久?
上下文是这样的...我有一个2按钮的ActionSheet弹出窗口,如果用户点击其中一个按钮,应用程序似乎停顿了大约一秒钟。下面的代码:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
UIAlertView* newTimerAlertView = [[[UIAlertView alloc] initWithTitle:@"Create New Timer"
message:@"Enter a name for your new indicator"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Create", nil] autorelease];
newTimerAlertView.tag = kNewTimer;
newTimerAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[newTimerAlertView show];
}
else if (buttonIndex == 1)
{
NSLog(@"ActionSheet button 2 tapped");
UIAlertView* newTallyAlertView = [[[UIAlertView alloc] initWithTitle:@"Create New Tally"
message:@"Enter a name for your new indicator"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Create", nil] autorelease];
newTallyAlertView.tag = kNewTally;
newTallyAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[newTallyAlertView show];
NSLog(@"end");
}
}
着眼于为简便起见,第二个按钮(虽然第一个按钮的工作方式),日志是这样的:
2012-01-25 20:35:46.330 ...[177:707] ActionSheet button 2 tapped
2012-01-25 20:35:47.194 ...[177:707] end
2012-01-25 20:35:56.154 ...[177:707] ActionSheet button 2 tapped
2012-01-25 20:35:56.180 ...[177:707] end
注意,我第一次尝试,在代码片段执行完成之前超过一秒,但第二次(以及所有后续时间)代码只需要30毫秒左右。
代码有问题吗?还是我需要用进度视图来弥补延迟?
谢谢!
编辑:这只发生在设备上时,从Xcode运行应用程序...也许这与调试器有关吗?
如果你关心调试放慢你失望,那么就使用一些'NSTimeInterval'实例变量,后来记录它们。例如:'startSecond = [NSDate timeIntervalSinceReferenceDate];'。已知NSLog会导致性能问题。 – NJones