ios不响应presentModalViewController界面的处理

   最近折腾ios的一个邮件分享和SMS短信分享功能,本来是很简单的的功能,但是我发现打开的邮件界面和短信界面一直不响应navigationController上面的按钮,感觉很奇怪的,最后发现,需要在打开的这些界面的委托实现方法中,把打开的视图关闭,说的有点别扭吧! 我发一段代码出来,大家可能会明白的!

注意:如果想调用发送短信和邮件的功能,必须要引入一个库:MessageUI.framework  

还需要import一些类:

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>


如下代码是ios发送短信的代码:
-(void)sendSMSComposerSheet
{
    MFMessageComposeViewController *messageCtrl = [[MFMessageComposeViewController alloc] init];
    messageCtrl.messageComposeDelegate = self;
    messageCtrl.body = @"share ....";
    [self presentModalViewController:messageCtrl animated:YES];
}

这段代码实现的委托就:MFMessageComposeViewControllerDelegate

可是我的代码中也实现了委托,代码如下:

 

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    
}
但是打开短信界面是没问题的,关键是点击短信界面的取消按钮没有相应,最后我想了一下,估计要在委托的实现方法中关闭打开的视图,结果的确如此!
以上代码修改为:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [controller dismissModalViewControllerAnimated:YES];
}

或者:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [controller dismissViewControllerAnimated:YES completion:^{}];
}

两种方式都可以的,这样就能解决ios使用presentModalViewController打开viewController,相应viewcongroller上面的相关按钮了!

同时在附加上 ios发送邮件的代码:

-(void)sendMailComposerSheet
{
    MFMailComposeViewController *mailCtrl = [[MFMailComposeViewController alloc] init];
    mailCtrl.mailComposeDelegate = self;
    [mailCtrl setSubject:@"邮件标题"];

    [mailCtrl setMessageBody:@"邮件内容" isHTML:NO];
    [self presentModalViewController:mailCtrl animated:YES];
}

相关的委托:MFMailComposeViewControllerDelegate

界面的处理
-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
     [controller dismissModalViewControllerAnimated:YES];
}




你可能感兴趣的:(ios不响应presentModalViewController界面的处理)