首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在关闭MFMessageController时显示AlertView时出错

在关闭MFMessageController时显示AlertView时出错
EN

Stack Overflow用户
提问于 2017-01-31 16:57:57
回答 1查看 30关注 0票数 0

我正在为视图层次结构而苦苦挣扎,但我不知道这里出了什么问题。我尝试发送一条消息(MFMessageComposeViewController)。我想展示一个依赖于委托方法的成功(或失败)的alertView。取消消息时会显示alertView,但发送消息时会出现以下错误:

代码语言:javascript
复制
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完成之前被调用,而不是在消息被取消时调用。如果它能帮上忙:

代码语言:javascript
复制
- (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];
}

如果有人能帮我..。

EN

回答 1

Stack Overflow用户

发布于 2017-01-31 17:39:15

您没有以正确的方式初始化UIAlertController。您正在使用相同的变量名alertController,该名称对

代码语言:javascript
复制
if (tag == 3) {
}

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

代码语言:javascript
复制
- (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];
}

希望能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41953333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档