4

我是 iphone 开发新手。我创建了一个基于标签栏的应用程序。首先,我希望显示电子邮件作曲家。我可以显示它,但是取消和发送按钮不起作用,我不知道我哪里出错了。请帮帮我。这是我的代码。

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [self displayComposerSheet];    
}

-(void)displayComposerSheet 
{
    picker = [[MFMailComposeViewController alloc] init];

   [[picker navigationBar] setTintColor:[UIColor blackColor]];

   picker.mailComposeDelegate = self;

   if ([MFMailComposeViewController canSendMail]) 
   {

            [picker setToRecipients:[NSArray arrayWithObjects:@"name@gmail.com",nil]];

            [picker setSubject:@"Sample"];

   }
   [self.view addSubview:picker.view];
   [self presentModalViewController:picker animated:YES];

}

 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{

    [self dismissModalViewControllerAnimated:YES];

 }
4

4 回答 4

1

如果您只添加 mailcomposser 的子视图,则必须将其从 self.view 中删除,在您的代码中,您正在添加子视图并同时呈现,

如果您只使用[self.view addSubview:picker.view];而不是尝试删除它。

 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{
    [controller.view removeFromSuperview];

 }

我仍然建议使用

[self.navigationController presentModalViewController:picker animated:YES];对于当前 MFMailComposeViewController ,

并用[self dismissModalViewControllerAnimated:YES];它来解雇它。

于 2013-12-11T06:02:57.307 回答
1

您将两次展示邮件编写器。

删除行:

[self.view addSubview:picker.view];

并将下一行替换为:

[self.navigationController presentModalViewController:picker animated:YES];
于 2010-03-27T21:05:49.230 回答
0

设置 MFMailComposeViewController 的委托

MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc]init];

mailcomposer.mailComposeDelegate = self; 

并使用此委托方法

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
}
于 2013-12-11T05:39:22.273 回答
0

使用此代码:

MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObjects:@"niftyapplications@gmail.com", @"support@niftysol.com", nil]; 
[controller setToRecipients:toRecipients];
[controller setTitle:@"Contact Us"];
controller.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:controller animated:YES];


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{
     [self becomeFirstResponder];
     NSString *strMailResult;
     switch (result)
     {
        case MFMailComposeResultCancelled:
        strMailResult = NSLocalizedString(@"E-Mail Cancelled",@"");
        break;
        case MFMailComposeResultSaved:
        strMailResult = NSLocalizedString(@"E-Mail Saved",@"");
        break;
        case MFMailComposeResultSent:
        strMailResult = NSLocalizedString(@"E-Mail Sent",@"");
        break;
        case MFMailComposeResultFailed:
        strMailResult = NSLocalizedString(@"E-Mail Failed",@"");
        break;
        default:
        strMailResult = NSLocalizedString(@"E-Mail Not Sent",@"");
        break;
     }

     UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ISO Audit",@"") message:strMailResult delegate:self  cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
     [alertView show];
    [self dismissModalViewControllerAnimated:YES];
}
于 2013-12-11T05:48:12.510 回答