2016-12-26 3 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를 추가합니다. e 헤더 파일에 ...? –

+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