在另一个UIAlertController原因中显示UIAlertController导致在取消分配时尝试加载视图控制器的视图是不允许的

时间:2016-05-11 13:48:47

标签: ios objective-c uialertview uiactionsheet uialertcontroller

我想提出UIAlertController UIAlertControllerStyleAlert样式,同时解除UIAlertController样式UIAlertControllerStyleActionSheet。换句话说,我想在从操作表中点击选项后显示警报视图。但是没有显示警报视图,只显示日志中的此消息:

  

尝试加载视图控制器的视图   不允许解除分配并可能导致未定义的行为

这是我的代码:

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

动作表正在运行,而警报视图没有。动作表来自UITableViewController。非常感谢每一位帮助,提前谢谢

2 个答案:

答案 0 :(得分:2)

修正了......代码。 您无需关闭以加载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)中添加了代码动画并且工作正常。

希望有所帮助

答案 1 :(得分:2)

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

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