MFMessageComposeVIewController 正在解除其委托而不是自身
MFMessageComposeVIewController is dismissing its delegate instead of itself
我有一个 VC 符合 MFMessageComposeViewControllerDelegate 协议。
我使用以下代码成功展示了这个视图控制器:
- (IBAction)textAction:(id)sender {
if(![MFMessageComposeViewController canSendText])
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[warningAlert show];
return;
}
NSString *numberToCallOrText = self.phoneNumber;
NSString *message = @"Test message";
NSArray *recipients = [NSArray arrayWithObject:numberToCallOrText];
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipients];
[messageController setBody:message];
// Present message view controller on screen
[self.view endEditing:YES];
[self presentViewController:messageController animated:YES completion:nil];
}
另外,我是这样处理完成结果的:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
switch (result) {
case MessageComposeResultCancelled:
NSLog(@"Canceled");
break;
case MessageComposeResultFailed:
{
NSLog(@"Failed");
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to send SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
NSLog(@"sent");
[self.navigationController popViewControllerAnimated:YES];
break;
default:
break;
}
[controller.view endEditing:YES];
[self.navigationController popViewControllerAnimated:YES];
// [self dismissViewControllerAnimated:YES completion:nil];
// [controller popToViewController:self animated:YES];
// [controller dismissViewControllerAnimated:YES completion:nil];
}
注释掉的三行是我试过的替代方案。发生的事情是 MFMessageComposeViewController 保留在屏幕上(尽管键盘已关闭),但委托正在从堆栈中弹出。因此,当我再次点击取消时,出现空引用错误。
这很奇怪,因为我的代码中的其他地方也可以使用相同的实现。唯一的区别是我已经设置了要初始化的主体。
知道为什么会在此处弹出错误的 VC 吗?
谢谢。
编辑 - 损坏的实现是在 UITableViewController 而不是 UIView Controller 上...这可能是导致问题的原因吗?
您正在使用 presentViewController:animated:completion:
显示消息编辑器。当视图控制器以这种方式显示时,必须使用 dismissViewControllerAnimated:completion:
.
将其关闭
但是您正在使用 popViewControllerAnimated:
,它告诉导航控制器关闭导航堆栈顶部的任何视图控制器。这不是消息编写器,而是呈现消息的视图控制器。
您接近注释掉的行之一。您需要更换:
[self.navigationController popViewControllerAnimated:YES];
与:
[controller dismissViewControllerAnimated:YES completion:nil];
您还需要删除以下行:
[self.navigationController popViewControllerAnimated:YES];
消息发送时。不要做任何额外的解雇。
请看下面的代码希望对你有用....
-(void) showEmailModalView
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController* controller
= [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setToRecipients: @[@"dept-em@np.edu.sg"]];
[self presentViewController: controller
animated: YES
completion: nil];
}
else
{
UIAlertController* alert = [UIAlertController
alertControllerWithTitle: @"Email not configured"
message: @"Please add/enable an email "
@"account in the phone to send email"
preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle: @"Ok"
style: UIAlertActionStyleDefault
handler: nil];
[alert addAction: ok];
[self presentViewController: alert
animated: YES
completion: nil];
}
}
- (void) mailComposeController : (MFMailComposeViewController*) controller didFinishWithResult : (MFMailComposeResult) result error :(NSError*) error
{
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
break;
default:
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Email" message:@"Sending Failed - Unknown Error :-(" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}
break;
}
[self dismissViewControllerAnimated:controller completion:nil];
}
问题似乎源于从 UITableViewController 而不是 UIViewController 调用 MFMessageComposeViewController。因此,我的解决方案是让 table 推送另一个视图控制器,其唯一目的是推送 MFMessageComposeViewController,并在 MFMessageComposeViewController 的完成处理程序中关闭自身。
我有一个 VC 符合 MFMessageComposeViewControllerDelegate 协议。
我使用以下代码成功展示了这个视图控制器:
- (IBAction)textAction:(id)sender {
if(![MFMessageComposeViewController canSendText])
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[warningAlert show];
return;
}
NSString *numberToCallOrText = self.phoneNumber;
NSString *message = @"Test message";
NSArray *recipients = [NSArray arrayWithObject:numberToCallOrText];
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipients];
[messageController setBody:message];
// Present message view controller on screen
[self.view endEditing:YES];
[self presentViewController:messageController animated:YES completion:nil];
}
另外,我是这样处理完成结果的:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
switch (result) {
case MessageComposeResultCancelled:
NSLog(@"Canceled");
break;
case MessageComposeResultFailed:
{
NSLog(@"Failed");
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to send SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
NSLog(@"sent");
[self.navigationController popViewControllerAnimated:YES];
break;
default:
break;
}
[controller.view endEditing:YES];
[self.navigationController popViewControllerAnimated:YES];
// [self dismissViewControllerAnimated:YES completion:nil];
// [controller popToViewController:self animated:YES];
// [controller dismissViewControllerAnimated:YES completion:nil];
}
注释掉的三行是我试过的替代方案。发生的事情是 MFMessageComposeViewController 保留在屏幕上(尽管键盘已关闭),但委托正在从堆栈中弹出。因此,当我再次点击取消时,出现空引用错误。
这很奇怪,因为我的代码中的其他地方也可以使用相同的实现。唯一的区别是我已经设置了要初始化的主体。
知道为什么会在此处弹出错误的 VC 吗?
谢谢。
编辑 - 损坏的实现是在 UITableViewController 而不是 UIView Controller 上...这可能是导致问题的原因吗?
您正在使用 presentViewController:animated:completion:
显示消息编辑器。当视图控制器以这种方式显示时,必须使用 dismissViewControllerAnimated:completion:
.
但是您正在使用 popViewControllerAnimated:
,它告诉导航控制器关闭导航堆栈顶部的任何视图控制器。这不是消息编写器,而是呈现消息的视图控制器。
您接近注释掉的行之一。您需要更换:
[self.navigationController popViewControllerAnimated:YES];
与:
[controller dismissViewControllerAnimated:YES completion:nil];
您还需要删除以下行:
[self.navigationController popViewControllerAnimated:YES];
消息发送时。不要做任何额外的解雇。
请看下面的代码希望对你有用....
-(void) showEmailModalView
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController* controller
= [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setToRecipients: @[@"dept-em@np.edu.sg"]];
[self presentViewController: controller
animated: YES
completion: nil];
}
else
{
UIAlertController* alert = [UIAlertController
alertControllerWithTitle: @"Email not configured"
message: @"Please add/enable an email "
@"account in the phone to send email"
preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle: @"Ok"
style: UIAlertActionStyleDefault
handler: nil];
[alert addAction: ok];
[self presentViewController: alert
animated: YES
completion: nil];
}
}
- (void) mailComposeController : (MFMailComposeViewController*) controller didFinishWithResult : (MFMailComposeResult) result error :(NSError*) error
{
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
break;
default:
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Email" message:@"Sending Failed - Unknown Error :-(" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}
break;
}
[self dismissViewControllerAnimated:controller completion:nil];
}
问题似乎源于从 UITableViewController 而不是 UIViewController 调用 MFMessageComposeViewController。因此,我的解决方案是让 table 推送另一个视图控制器,其唯一目的是推送 MFMessageComposeViewController,并在 MFMessageComposeViewController 的完成处理程序中关闭自身。