在另一个 UIAlertController 中呈现 UIAlertController 导致不允许在取消分配时尝试加载视图控制器的视图

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

我想呈现 UIAlertControllerStyleAlert 风格的 UIAlertController,同时拒绝 UIAlertControllerStyleActionSheet 风格的 UIAlertController。换句话说,我想在点击操作表中的选项后显示一个警报视图。但是没有出现alertview,日志中只出现了这条信息:

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

这是我的代码:

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

actionsheet 正在运行,alertview 没有。操作表来自 UITableViewController。非常感谢您的帮助,提前致谢

已修复..这是代码。 您无需关闭即可加载 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];

刚刚通过在 - (void)viewDidAppear:(BOOL)animated 中添加该代码在空项目上进行了尝试,效果很好。

希望对您有所帮助

问题是您在 [self dismissViewControllerAnimated] 的完成块中调用 [self presentViewController]。

您尝试显示的警报视图控制器正在被释放,因为它是自身视图控制器中的局部变量,已被解除。