Search code examples
iosobjective-cuialertviewuiactionsheetuialertcontroller

Present UIAlertController in another UIAlertController cause Attempting to load the view of a view controller while it is deallocating is not allowed


I want to present a UIAlertController with UIAlertControllerStyleAlert style while dismissing a UIAlertController with UIAlertControllerStyleActionSheet style. In other words, I want to present an alertview after tapping an option from an actionsheet. But no alertview is presented, only this message in the log appears:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

Here my code:

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"MyApp" message:@"Info" preferredStyle:UIAlertControllerStyleActionSheet];

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        // Cancel button tappped.
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }]];

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Credits" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        UIAlertController * alert=   [UIAlertController
                                      alertControllerWithTitle:@"Credits"
                                      message:@"Here some text in the alertview..."
                                      preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* yesButton = [UIAlertAction
                                    actionWithTitle:@"Close"
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action)
                                    {
                                        //Handel your yes please button action here


                                    }];

        [alert addAction:yesButton];

        [self dismissViewControllerAnimated:YES completion:^{
            // try to present the alertview
            [self presentViewController:alert animated:YES completion:nil];
        }];
    }]];

    // Present action sheet.
    [self presentViewController:actionSheet animated:YES completion:nil];

The actionsheet is working, the alertview not. The actionsheet is presented from a UITableViewController. Every help is greatly appreciated, thanks in advance


Solution

  • Fixed..here is the code. You do not need to dismiss in order to load the the AlertController.

    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"MyApp" message:@"Info" preferredStyle:UIAlertControllerStyleActionSheet];
    
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    
        // Cancel button tappped.
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }]];
    
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Credits" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    
        UIAlertController * alert=   [UIAlertController
                                      alertControllerWithTitle:@"Credits"
                                      message:@"Here some text in the alertview..."
                                      preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction* yesButton = [UIAlertAction
                                    actionWithTitle:@"Close"
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action)
                                    {
                                        //Handel your yes please button action here
    
    
                                    }];
    
        [alert addAction:yesButton];
    
        [self presentViewController:alert animated:YES completion:nil];
    }]];
    
    // Present action sheet.
    [self presentViewController:actionSheet animated:YES completion:nil];
    

    Just tried that on an empty project by adding that code in - (void)viewDidAppear:(BOOL)animated and works fine.

    Hope that helps