Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 递归地构建UIAlertController并呈现它_Ios_Recursion_Uialertcontroller - Fatal编程技术网

Ios 递归地构建UIAlertController并呈现它

Ios 递归地构建UIAlertController并呈现它,ios,recursion,uialertcontroller,Ios,Recursion,Uialertcontroller,我想用UIAlertControllerStyleActionSheet一个接一个地展示一个UIAlertController。为此,我必须在我的UIAlertAction的处理程序中显示下一个UIAlertController UIAlertController *A = [UIAlertController alertControllerWithTitle:@"Alert A" message:@"My Alert A" preferredStyle:UIAlertControllerSty

我想用
UIAlertControllerStyleActionSheet
一个接一个地展示一个
UIAlertController
。为此,我必须在我的
UIAlertAction
的处理程序中显示下一个
UIAlertController

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];

有没有一种递归的方法

您只需要一个方法来显示一个警报控制器。在操作处理程序中,只需调用相同的方法即可显示下一个警报

诀窍在于知道何时停止并处理每个警报的消息和按钮。下面的代码假设您有一些实例变量,这些变量包含一种根据每个警报的编号访问消息和标题的方法。它还假设每个警报都有一个按钮,即转到下一个警报的按钮

大致如下:

- (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];
-(void)shownestalert:(NSInteger)计数{
NSString*消息=…//确定警报n的消息
NSString*title=…//确定警报n的标题
NSString*按钮=…//确定警报n的操作标题
UIAlertController*alert=[UIAlertController alertControllerWithTitle:title消息:消息首选样式:UIAlertControllerStyleActionSheet];
UIAlertAction*action=[UIAlertAction actionWithTitle:按钮样式:UIAlertActionStyleDefault处理程序:^(UIAlertAction*action){
//处理警报n的按钮
//检查是否应显示另一个警报
如果(计数
这段代码没有经过测试,但应该能给你一个大概的想法