如何在显示UIAlertView后停止代码执行,直到用户按下OK按钮?如果这是一个问题,那么解决方法是什么?
发布于 2012-08-27 21:22:31
最终使用的是:
..。
[alert show];
while ((!alert.hidden) && (alert.superview != nil))
{
[[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];
}发布于 2012-08-26 23:12:58
看起来您并不想执行在alertview show方法之后编写的代码。要实现这一点,请将这些代码行添加到一个方法中,并在下面的UIAlertView委托中调用该方法。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == OKButtonIndex)
{
// Issue a call to a method you have created that encapsulates the
// code you want to execute upon a successful tap.
// [self thingsToDoUponAlertConfirmation];
}
}现在,如果您的类中将有多个UIAlertView,则需要确保可以轻松地处理每个UIAlertView。您可以在UIAlertView上使用NSEnum和tag设置来完成此操作。
如果您有三个警报,请在类的顶部在@接口之前声明一个NSEnum,如下所示:
// alert codes for alertViewDelegate // AZ 09222014
typedef NS_ENUM(NSInteger, AlertTypes)
{
UserConfirmationAlert = 1, // these are all the names of each alert
BadURLAlert,
InvalidChoiceAlert
}; 然后,在警报显示之前,将警报的标记设置为显示。
myAlert.tag = UserConfirmationAlert;然后在您的UIAlertDelegate中,您可以在开关/用例中执行所需的方法,如下所示:
// Alert handling code
#pragma mark - UIAlertViewDelegate - What to do when a dialog is dismissed.
// We are only handling one button alerts here. Add a check for the buttonIndex == 1
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (alertView.tag) {
case UserConfirmationAlert:
[self UserConfirmationAlertSuccessPart2];
alertView.tag = 0;
break;
case BadURLAlert:
[self BadURLAlertAlertSuccessPart2];
alertView.tag = 0;
break;
case InvalidChoiceAlert:
[self InvalidChoiceAlertAlertSuccessPart2];
alertView.tag = 0;
break;
default:
NSLog(@"No tag identifier set for the alert which was trapped.");
break;
}
}https://stackoverflow.com/questions/12129703
复制相似问题