在我的应用程序要显示的UIAlertView中的for循环UIAlertView中后暂停执行,并基于选择“是”,“否”要显示在for循环
我想执行下一步骤。由于UIAlertView不会暂停执行,我无法处理这种情况。反制全局和所有将使我的代码更复杂。所以,我想暂停执行直到用户选择警报按钮。
请让我知道任何解决方案。
谢谢。
在我的应用程序要显示的UIAlertView中的for循环UIAlertView中后暂停执行,并基于选择“是”,“否”要显示在for循环
我想执行下一步骤。由于UIAlertView不会暂停执行,我无法处理这种情况。反制全局和所有将使我的代码更复杂。所以,我想暂停执行直到用户选择警报按钮。
请让我知道任何解决方案。
谢谢。
首先添加<UIAlertViewDelegate>
您.h
文件为您的视图控制器:
@interface ViewController : UIViewController <UIAlertViewDelegate> {
然后创建警报,并显示它当你需要它:
- (void)showConfirmAlert
{
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:@"Confirm"];
[alert setMessage:@"Do you pick Yes or No?"];
[alert setDelegate:self]; // Notice we declare the ViewController as the delegate
[alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
[alert show];
[alert release];
}
并实现委托方法捉按钮点击:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
// Yes, do something
}
else if (buttonIndex == 1)
{
// No
}
}
Voila,yo你可以处理这两种情况。
编辑:如果你有许多警报,宣布他们都为全局对象,所以你可以在alertView:clickedButtonAtIndex:
方法每一个区分:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView == alertOne) { //alertOne is a globally declared UIAlertView
if (buttonIndex == 0)
{
// Yes, do something
}
else if (buttonIndex == 1)
{
// No
}
} else if (alertView == alertTwo) { //alertTwo is a globally declared UIAlertView
if (buttonIndex == 0)
{
// Yes, do something
}
else if (buttonIndex == 1)
{
// No
}
}
}
当显示AlertView打破你的循环到时候再在用户选择上再次运行循环或进一步执行。顺便说一下你想实现的目标是什么?
利亚姆乔治Betsworth,感谢您的答复,但我知道这种传统的做法。但我想在循环中做到这一点,所以它不可能处理循环条件和所有。 – 2012-07-17 11:19:23