我希望一个接一个地UIAlertController
UIAlertControllerStyleActionSheet
。为此,我必须在UIAlertController
的处理程序中显示下一个UIAlertAction
。
UIAlertController *A = [UIAlertController alertControllerWithTitle:@"Alert A" message:@"My Alert A" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* A_Action = [UIAlertAction actionWithTitle:@"A Action" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
UIAlertController *B = [UIAlertController alertControllerWithTitle:@"Alert B" message:@"My Alert B" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* B_Action = [UIAlertAction actionWithTitle:@"B Action" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
UIAlertController *C = [UIAlertController alertControllerWithTitle:@"Alert C" message:@"My Alert C" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* C_Action = [UIAlertAction actionWithTitle:@"C Action" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
//keep going for N number of UIAlertControllers that I want to present
}];
[alert addAction:C_Action];
[self presentViewController:C animated:YES completion:nil];
}];
[alert addAction:B_Action];
[self presentViewController:B animated:YES completion:nil];
}];
[alert addAction:A_Action];
[self presentViewController:A animated:YES completion:nil];
有没有办法以递归方式执行此操作?
答案 0 :(得分:0)
您只需要一种显示一个警报控制器的方法。在动作处理程序中,您只需调用相同的方法来显示下一个警报。
诀窍是知道何时停止并处理每个警报的消息和按钮。以下代码假定您有一些实例变量,其中包含根据其编号访问每个警报的消息和标题的方法。它还假设每个警报都有一个按钮 - 即进入下一个警报的按钮。
有些事情如下:
- (void)showNextAlert:(NSInteger)count {
NSString *message = ... // determine the message for alert n
NSString *title = ... // determine the title for alert n
NSString *button = ... // determine the title of the action for alert n
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action = [UIAlertAction actionWithTitle:button style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Process the button for alert n
// Check to see if another alert should be shown or not
if (count < self.maxAlerts) {
// Give the current alert a chance to dismiss
dispatch_async(dispatch_get_main_queue(), ^(void){
[self showNextAlert:count + 1];
});
}
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
// Elsewhere, show the 1st alert
[self showNextAlert:0];
此代码未经过测试,但应该为您提供一般概念。