我正在 actionSheet:clickedButtonAtIndex 委托(delegate)方法上创建一个 UIActionSheet
。
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
if(buttonIndex == 1){
[self.myFirstView removeFromSuperview];
if (!self.mySecondView) {
[[NSBundle mainBundle] loadNibNamed:@"MySecondView" owner:self options:nil];
}
[self.mySecondView setFrame:CGRectMake(0, 0, 320, 480)];
[[UIApplication sharedApplication].keyWindow addSubview: self.mySecondView];
UIActionSheet * action = [[UIActionSheet alloc]initWithTitle:@""
delegate:self
cancelButtonTitle: nil
destructiveButtonTitle: deleteContacts
otherButtonTitles: cancel, nil];
action.tag = 102;
[action showInView:self.view];
[action release];
}
我用与上面完全相同的方法处理此 UIActionSheet
的点击事件。
if(actionSheet.tag == 102){
if(buttonIndex == 0){
if([[NSBundle mainBundle] loadNibNamed:@"MyThirdView" owner:self options:nil]) {
[self.myThirdView setFrame:CGRectMake(0, 0, 320, 480)];
[[UIApplication sharedApplication].keyWindow addSubview:self.myThirdView];
}
[self.mySecondView removeFromSuperview];
[self.doneButton.target performSelector:self.doneButton.action withObject:self.doneButton.target];
[self performSelector:@selector(RemoveView) withObject:self afterDelay:3.0];
}
}
我面临的问题是,UIActionSheet
响应时间过长。当我单击 UIActionSheet
按钮时,它会在 myThirdView
加载之前处于卡住状态 2 或 3 秒。我无法理解,在这种情况下,响应延迟是多少,因为我在 UIActionSheet
按钮单击事件方法中做的第一件事是加载 myThirdView
。其余代码仅在加载 myThirdView 的代码之后执行。但即使是第一行代码似乎也要延迟执行。
有什么建议吗?
最佳答案
这可能是因为这个
[self performSelector:@selector(RemoveView) withObject:self afterDelay:3.0];
制作其他方法并在该方法中执行此操作。像这样
[self viewRemover];
在 viewRemover 中
-(void) viewRemover
{
[self performSelector:@selector(RemoveView) withObject:self afterDelay:3.0];
}
所以你的代码现在是这样的
if(actionSheet.tag == 102){
if(buttonIndex == 0){
if([[NSBundle mainBundle] loadNibNamed:@"MyThirdView" owner:self options:nil]) {
[self.myThirdView setFrame:CGRectMake(0, 0, 320, 480)];
[[UIApplication sharedApplication].keyWindow addSubview:self.myThirdView];
}
[self.mySecondView removeFromSuperview];
[self.doneButton.target performSelector:self.doneButton.action withObject:self.doneButton.target];
[self performSelectorInBackground:@selector(viewRemover) withObject:nil];
}
}
关于iphone - UIActionSheet 需要很长时间才能响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11030114/