【问题标题】:Present UIAlertController in another UIAlertController cause Attempting to load the view of a view controller while it is deallocating is not allowed在另一个 UIAlertController 中显示 UIAlertController 导致不允许在解除分配时尝试加载视图控制器的视图
【发布时间】:2016-05-11 13:48:46
【问题描述】:

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

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

这是我的代码:

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。非常感谢您的每一个帮助,在此先感谢

【问题讨论】:

    标签: ios objective-c uialertview uiactionsheet uialertcontroller


    【解决方案1】:

    已修复..这里是代码。 您无需关闭即可加载 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 中添加该代码在一个空项目上进行了尝试,并且工作正常。

    希望有帮助

    【讨论】:

    • 我犯了多么愚蠢的错误……谢谢!还要感谢@Mike Taverna
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      相关资源
      最近更新 更多