iphone UIAlertView Modal

ghi*_*boz 10 iphone modal-dialog objective-c uialertview

是否可以呈现UIAlertView并且不会继续执行该方法中的其余代码,直到用户响应警报?

提前致谢.

iPr*_*abu 20

我想停止你想要的代码是停止设备运行你在alertview之后编写的下一个代码

为此,只需在alertview之后删除您的代码,并将该代码放在alertview委托中

-(void) yourFunction
{
     //Some code
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Your Message" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
            [alert release];
     //Remove all your code from here put it in the delegate of alertview
}
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:    (NSInteger)buttonIndex 
{
if(buttonIndex==0)
    {
        //Code that will run after you press ok button 
    }
}
Run Code Online (Sandbox Code Playgroud)

别忘了在.h文件中包含UIAlertViewDelegate

  • 啊.有点难过这是解决这个问题的最好方法."哎呀,我刚刚意识到我应该确保用户在执行代码之前想要这样做.现在让我重新组织我的所有代码......" (4认同)

BWW*_*BWW 9

答案已被接受,但我会注意到遇到此问题的任何人,虽然您不应该将此用于正常的警报处理,但在某些情况下,您可能希望阻止当前执行路径在警报时继续正在呈现.为此,您可以在主线程的运行循环中旋转.

我使用这种方法来处理我想在崩溃之前呈现给用户的致命错误.在这种情况下,发生了灾难性的事情,所以我不想从导致错误的方法返回,这可能允许其他代码以无效状态执行,例如,损坏的数据.

请注意,这不会阻止事件被处理或阻止其他线程运行,但由于我们正在呈现一个基本上接管接口的警报,因此事件通常应限于该警报.

// Present a message to the user and crash
-(void)crashNicely {

  // create an alert
  UIAlertView *alert = ...;

  // become the alert delegate
  alert.delegate = self;

  // display your alert first
  [alert show];

  // spin in the run loop forever, your alert delegate will still be invoked
  while(TRUE) [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];

  // this line will never be reached
  NSLog(@"Don't run me, and don't return.");

}

// Alert view delegate
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
  abort(); // crash here
}
Run Code Online (Sandbox Code Playgroud)