我正在为视图层次结构而苦苦挣扎,但我不知道这里出了什么问题。我尝试发送一条消息(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];
}如果有人能帮我..。
发布于 2017-01-31 17:39:15
您没有以正确的方式初始化UIAlertController。您正在使用相同的变量名alertController,该名称对
if (tag == 3) {
}但不是对该方法公开的。并且您使用的变量名对方法是公共的,[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];
}希望能对你有所帮助。
https://stackoverflow.com/questions/41953333
复制相似问题