2016-12-26 3 views
0

Ich habe eine Klasse, die Unterklasse NSObject mit einer Funktion einen MFMailComposeViewController anzeigen. Hier ist der Code:MFMailComposeViewController nicht mit Abbrechen Abbrechen Schaltfläche

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

mailController.mailComposeDelegate = self; 
[mailController setSubject:@"Sample Subject"]; 
[mailController setMessageBody:@"Here is some main text in the email!" isHTML:NO]; 
[mailController setToRecipients:@[self.email]]; 

UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController; 
UINavigationController *navigationController = tabbarController.selectedViewController; 
[navigationController.topViewController presentViewController:mailController animated:YES completion:NULL]; 

Alles funktioniert gut mit diesem Code. Das Problem ist, wenn ich die MFMailComposeViewController ablehnen möchte. Irgendwann bekomme ich einen Unfall, manchmal passiert einfach nichts. Ich habe die Delegierten Funktion implementiert:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { 
    UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController; 
    UINavigationController *navigationController = tabbarController.selectedViewController; 
    [navigationController.topViewController dismissViewControllerAnimated:YES completion:nil]; 
} 

Danach habe ich versucht, es zu zeigen und entlassen directlty von einem Viewcontroller und alles funktioniert. Sogar die Abbrechen-Taste.

Ich weiß nicht, warum es in meiner ViewController-Klasse funktioniert, aber nicht in meiner Unterklasse von NSObject.

Wenn ich den Absturz ich in den Protokollen gesehen habe:

-[MFMailComposeInternalViewController _notifyCompositionDidFinish] 
+0

haben Sie fügen MFMailComposeViewDelegat hinzu e in der Header-Datei ...? –

+0

Ja, habe ich. Nun, ich habe es in der .m-Datei hinzugefügt, aber es ist dasselbe. –

+0

Geben Sie einige Informationen zum Crash-Protokoll ein, um weitere Hilfe zu erhalten. –

Antwort

0

die Sie interessieren,

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{ 
    // Do your work and dismiss after you are done with it. 
    [controller dismissViewControllerAnimated:YES completion:nil]; 
} 

Hoffnung, das hilft.

+0

Danke für Ihren Kommentar Naresh, aber ich vergesse zu spezifizieren, dass die Delegate-Methode nicht aufgerufen wird. –

+0

Delegierter Delegierter bei der Präsentation von Mail-Composer? –

0

Versuchen Sie dies in Ihrer Singletonklasse

UIViewController *currentViewController; 

    - (void)sendEmail:(id) viewController { 

currentViewController=(UIViewController*)viewController; 

     NSString * appVersionString [email protected]""; 
     NSString *[email protected]""; 
     NSString *[email protected]""; 


     NSArray *toRecipents [email protected]""; 



     if ([MFMailComposeViewController canSendMail]) { 

      MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
      mc.mailComposeDelegate =viewController; 
      [mc setSubject:strEmailSubject]; 
      [mc setMessageBody:strEmailMessage isHTML:NO]; 
      [mc setToRecipients:toRecipents]; 


      [viewController presentViewController:mc animated:YES completion:NULL]; 


     } 
     else{ 
      UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" message:@"Please setup email account" delegate:nil cancelButtonTitle:@"cancle" otherButtonTitles:nil]; 

      [alert show]; 
     } 

    } 

Satz Delegierten wie folgt

mc.mailComposeDelegate =viewController; 

dissmiss Viewcontroller

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

       break; 
      case MFMailComposeResultSaved: 
       break; 
      case MFMailComposeResultSent: 

       break; 
      case MFMailComposeResultFailed: 

       break; 
     } 

     // Close the Mail Interface 
     [currentViewController dismissViewControllerAnimated:YES completion:NULL]; 
    } 

ich das hoffentlich arbeiten ...

+0

Dhiru. Ok, es funktioniert, aber ich muss die Delegate-Methode in der ViewController-Klasse setzen. Ich kann keine NSObject-Klasse als Delegat festlegen. So seltsam. –

+0

Sie können diese Methode in die AppDelegate.m-Klasse schreiben und alle Delegate-Protokolle implementieren und den Delegaten auf 'self' setzen, wenn Sie AppDelegate.h in ViewController importieren. – Dhiru

Verwandte Themen