我希望能够从我的应用发送一封电子邮件,其中我已经完成了该部分工作。问题是当我取消发送电子邮件时(这是我现在正在测试的)电子邮件部分被解雇但我留下了黑屏,我似乎无法解雇。
所以这就是我所拥有的。我已经创建了一个类来处理电子邮件部分:
MailViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface MailViewController : UIViewController <MFMailComposeViewControllerDelegate>
@end
实施如下:
#import "MailViewController.h"
@interface MailViewController ()
@end
@implementation MailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
[mailer setMailComposeDelegate:self];
[mailer setSubject:@"Subject"];
NSMutableArray *toArray = [[NSMutableArray alloc] initWithObjects:@"email@gmail.com", nil];
[mailer setToRecipients:toArray];
[self presentViewController:mailer animated:YES completion:nil];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failuer" message:@"This device is unable to send emails" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result){
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled: you cancelled the operation and no email was sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail failed");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
default:
NSLog(@"Mail not sent");
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}
@end
所有这些都从另一个视图中显示出来:
-(IBAction)sendMessage:(id)sender{
NSLog(@"Going to send an email....");
MailViewController *mail = [[MailViewController alloc] init];
[self.navigationController pushViewController:mail animated:YES];
}
因此,当我运行所有这些时,电子邮件程序会打开,我可以发送电子邮件。我可以选择取消,然后删除草稿,删除电子邮件程序。但是,我留下了黑屏,然后我可以从顶部导航栏中选择返回以返回上一个视图。
我只是希望应用程序返回到显示电子邮件程序时发送或取消(或其他)电子邮件程序的视图。我确信这很简单,我很遗憾。
答案 0 :(得分:1)
问题是你是pushing
一个MailViewController然后是dimissing
,如果你push
你应该pop
视图控制器。
MFMailComposeViewController
应以模态方式呈现,而不是推送到导航堆栈上。您也不需要子类,您可以直接创建MFMailComposeViewController
的实例:
-(IBAction)sendMessage:(id)sender{
NSLog(@"Going to send an email....");
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[self presentViewController:mail animated:YES completion:nil];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result){
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled: you cancelled the operation and no email was sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail failed");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
default:
NSLog(@"Mail not sent");
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}
答案 1 :(得分:0)
我有几个架构问题,但首先是TL; DR版本:
[self.navigationController popViewControllerAnimated:NO];
您也可以为布尔值选择YES,但是根据您的实现来判断,这不会产生好的结果。把它放在代表电话中完成。
然而,为什么不把任何推动MailViewController推入堆栈处理作为MFMailComposeViewControllerDelegate的工作呢?你在这里什么都不做,需要一个全新的视图控制器被推入堆栈。