2016-12-26 18 views
0

NSObjectをサブクラス化するクラスには、MFMailComposeViewControllerを表示する関数があります。コードは次のとおりです。MFMailComposeViewControllerはキャンセルボタンで閉じることはありません

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]; 

すべてがこのコードでうまくいきます。問題は私がMFMailComposeViewControllerを却下したいときです。時々私はクラッシュする、時にはそれは何も起こらない。私が表示され、ViewControllerをからそれをdirectlty解任しようとしましたが、すべてが働いていた。その後

- (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]; 
} 

:私は、デリゲートの機能を実装しました。キャンセルボタンさえ。

私のViewControllerクラスでは動作しますが、NSObjectのサブクラスでは動作しません。

私は、ログで見てきたクラッシュを取得:

-[MFMailComposeInternalViewController _notifyCompositionDidFinish] 
+0

が持っている...この

mc.mailComposeDelegate =viewController; 

の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]; } 

をdissmissする

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]; } } 

セットデリゲートのようなあなたのシングルトンクラスでこれを試してみてくださいMFMailComposeViewDelegatを追加するヘッダファイル内の...? –

+0

はい、私はしました。さて、私は.mファイルに追加しましたが、それは同じです。 –

+0

さらなるヘルプのために、クラッシュログに関するいくつかの情報を掲載してください。 –

答えて

0

- (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]; 
} 

、これを試してみてください役に立てば幸いです。

+0

あなたのコメントをありがとうNareshですが、デリゲートメソッドが呼び出されていないことを忘れてしまいます。 –

+0

メール作成者を紹介している間に代議員が割り当てられていますか? –

0

+0

Dhiru。それは動作しますが、私はViewControllerクラスにデリゲートメソッドを配置する必要があります。 NSObjectクラスをデリゲートとして設定することはできません。とても奇妙。 –

+0

このメソッドをAppDelegate.mクラスに書き込み、すべてのデリゲートプロトコルを実装し、デリゲートを 'self'に設定すると、ViewControllerにAppDelegate.hをインポートすると、すべてのViewControllerからそのメソッドを使用できるようになります。 – Dhiru

関連する問題