【问题标题】:Error when presenting AlertView on dismissing MFMessageController在关闭 MFMessageController 时显示 AlertView 时出错
【发布时间】:2017-01-31 08:57:56
【问题描述】:

我在视图层次结构中苦苦挣扎,但我无法弄清楚这里出了什么问题。我尝试发送消息 (MFMessageComposeViewController)。我想在委托方法的帮助下根据成功(或不成功)显示alertView。取消消息时会显示 alertView 但发送消息时出现此错误:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x153b3c00>)

看起来好像在发送消息时(好像还有一些东西在幕后运行),在 MFMessageComposeView 被完全解除(或解除分配)之前调用了完成块,但在它被取消时却没有。 如果有帮助:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {

    int tag;

    switch (result) {
        case MessageComposeResultCancelled:
        {
            tag = 1;
            NSLog(@"Cancelled");
            break;
        }
        case MessageComposeResultFailed:
        {
            tag = 2;
            NSLog(@"Failed");
            break;
        }

        case MessageComposeResultSent:
        {
            tag = 3;
            NSLog(@"Sent");
            break;
        }
        default:
            break;
    }
    [controller dismissViewControllerAnimated:YES completion:^{
        [self alertSMSMessageWithTag:tag];
    }];
}

- (void)alertSMSMessageWithTag:(int)tag {

    UIAlertController *alertController;

    if (tag == 1) { // cancelled
        alertController = [UIAlertController alertControllerWithTitle:@"Message cancelled" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];
    }
    if (tag == 2) { // Failed
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Message failed" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];
    }
    if (tag == 3) { // sent
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Message sent" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];
    }
    [self presentViewController:alertController animated:YES completion:nil];
}

如果有人可以帮助我...

【问题讨论】:

    标签: objective-c delegates uialertview mfmessagecomposeview


    【解决方案1】:

    您没有以正确的方式初始化UIAlertController。您正在使用对

    公开的相同变量名 alertController
    if (tag == 3) {
    }
    

    但不对方法公开。而且您正在使用相同的变量名,该变量名对方法 UIAlertController *alertController; 是公开的 [self presentViewController:alertController animated:YES completion:nil]; 正在访问方法变量的公共变量,即 UIAlertController *alertController;,您已为标签 1 正确初始化。我建议您使用以下代码 -

    - (void)alertSMSMessageWithTag:(int)tag {
    
         UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];
        if (tag == 1) { // cancelled
           alertController.title = @"Message cancelled";
    
        }
        if (tag == 2) { // Failed
            alertController.title = @"Message failed";
        }
        if (tag == 3) { // sent
           alertController.title = @"Message sent";
    
        }
        [self presentViewController:alertController animated:YES completion:nil];
    }
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      相关资源
      最近更新 更多