Iphone 如何在已安装的modalViewController中显示MFMailComposeViewController?

Iphone 如何在已安装的modalViewController中显示MFMailComposeViewController?,iphone,objective-c,ios4,Iphone,Objective C,Ios4,我有一个modalViewController,上面有一个叫做email的按钮。按下此按钮后,将以模式显示MFMailComposeViewController。当我发送电子邮件时,一切都很好。但是当用户点击MFMail视图上的cancel按钮时,应用程序会挂断,并且不会显示操作表。它在控制台等中没有显示任何细节,显然在这种情况下没有调用MFMailCompose委托 我在另一个视图控制器(不是modalviewcontroller)中使用了相同的代码进行了检查,发现那里的一切都很好。因此,我假

我有一个modalViewController,上面有一个叫做email的按钮。按下此按钮后,将以模式显示MFMailComposeViewController。当我发送电子邮件时,一切都很好。但是当用户点击MFMail视图上的cancel按钮时,应用程序会挂断,并且不会显示操作表。它在控制台等中没有显示任何细节,显然在这种情况下没有调用MFMailCompose委托

我在另一个视图控制器(不是modalviewcontroller)中使用了相同的代码进行了检查,发现那里的一切都很好。因此,我假设问题在于在已经是modalviewcontroller的控制器中呈现MFMailComposeViewController

很长一段时间以来,我一直在浏览一些帮助,得出的结论是,由于MFMailComposeViewController本身就是一个modalviewcontroller,因此它只能在简单的视图控制器上使用,而不能在modalviewcontroller中工作

有人可以指导我如何在modalviewcontroller中使用MFMailComposer吗。任何帮助都将不胜感激

我的代码:

    -(IBAction)emailButtonPressed:(id)sender{

           Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
       if (mailClass != nil)
          {

          if ([mailClass canSendMail])
            {
              [self displayComposerSheet];
            }
          else
            {
              [self launchMailAppOnDevice];
            }
          }
        else
          {
            [self launchMailAppOnDevice];
          }


}


#pragma mark -
#pragma mark Compose Mail


-(void)displayComposerSheet 
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"Ilusiones"];


    // Set up recipients
     NSArray *toRecipients = [NSArray arrayWithObject:@"anam@semanticnotion.com"]; 

     [picker setToRecipients:toRecipients];
     // Attach a screenshot to the email      
     UIGraphicsBeginImageContext(self.view.bounds.size);
     [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();

         NSData *myData = UIImagePNGRepresentation(viewImage);
     [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"viewImage"];



     // Fill out the email body text
     NSString *emailBody = @"";
     [picker setMessageBody:emailBody isHTML:NO];

     [self presentModalViewController:picker animated:YES];
         [picker release];

 }



 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
 {  
   NSLog (@"entered the delegate"); // this line is not shown when the cancel button is tapped-meaning it never enters the delegate in that case.
  switch (result)
  {
case MFMailComposeResultCancelled:
    NSLog(@"Result: canceled");
    break;
case MFMailComposeResultSaved:
    NSLog(@"Result: saved");
    break;
case MFMailComposeResultSent:
    NSLog( @"Result: sent");
    break;
case MFMailComposeResultFailed:
    NSLog( @"Result: failed");
    break;
default:
    NSLog(@"Result: not sent");
    break;
 }
 [self dismissModalViewControllerAnimated:YES]; //this works fine in the send case, and the email is sent. but hangs in the cancel case.
}


#pragma mark -
#pragma mark Workaround


-(void)launchMailAppOnDevice
{
NSString *recipients = @"mailto:anam@semanticnotion.com.com?cc=second@example.com,third@example.com&subject=illusions!";
NSString *body = @"&body=xyz";

NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}

对我来说,这两种方式都有效。确保已将VC声明为MFMailComposeViewController的委托:

@interface MyViewController : UIViewController <MFMailComposeViewControllerDelegate>
@接口MyViewController:UIViewController
取消的索引为0 btw:


这两种方法对我都有效。确保已将VC声明为MFMailComposeViewController的委托:

@interface MyViewController : UIViewController <MFMailComposeViewControllerDelegate>
@接口MyViewController:UIViewController
取消的索引为0 btw: