短信窗口弹出后,我无法再次关闭它。取消按钮不起作用,发送按钮将发送短信但不关闭窗口。

我已经浏览了许多类似的问题,他们都建议要么实现 messageComposeViewController 方法,要么将 messageComposeDelegate 设置为 self 但我做了这两件事,但仍然没有。

谁能告诉我我做错了什么?

.h 文件

#import <MessageUI/MFMessageComposeViewController.h>
#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate,MFMessageComposeViewControllerDelegate,UINavigationControllerDelegate>{
  CLLocationManager *locationManager;
  MKMapView *mapView_;

}

.m 文件
- (IBAction)SendTextTapped:(id)sender{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    if([MFMessageComposeViewController canSendText])
    {
        controller.body = @"Hello!";
        controller.recipients = [NSArray arrayWithObjects:@"123456", nil];
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:nil];
    }
}


- (void)messageComposeViewController:(MFMessageComposeViewController*) controller didFinishWithResult:(MessageComposeResult)result error:(NSError*)error;
{
  NSLog(@"Entered messageComposeController");
  switch (result) {
    case MessageComposeResultSent: NSLog(@"SENT"); [self dismissViewControllerAnimated:YES completion:nil]; break;
    case MessageComposeResultFailed: NSLog(@"FAILED"); [self dismissViewControllerAnimated:YES completion:nil]; break;
    case MessageComposeResultCancelled: NSLog(@"CANCELLED"); [self dismissViewControllerAnimated:YES completion:nil]; break;
  }
}

最佳答案

删除委托(delegate)方法上的错误参数。委托(delegate)方法应该是:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    // code here
}

不是:
- (void)messageComposeViewController:(MFMessageComposeViewController*) controller didFinishWithResult:(MessageComposeResult)result error:(NSError*)error
{
    // code here
}

10-06 14:45