我这里有一些我正在开发的 iOS 应用程序的代码,出于某种原因,我的 iPhone 4S 每次都需要整整一秒或更长时间才能完成看似快速而简单的任务。
上下文是这样的...我有一个 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 毫秒左右。
代码有问题吗?还是我只需要用进度 View 来弥补延迟?
谢谢!
编辑:这仅在设备上从 Xcode 运行应用程序时发生...可能与调试器有关?
最佳答案
我现在如果警报不在主线程上,它们可能会延迟
关于objective-c - 为什么实例化 UIAlertView 需要这么长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9012985/