1

I have a table view with four sections. The cell of one of the sections contains the email address. When i click on that cell i open mail view controller. Now when i click on the cancel button which is present on the navigation bar, an action sheet appears in which there are three buttons. One of those three buttons is cancel button. Now i want to return back to the table view when i click on this cancel button of action sheet. i have tried all the possible methods including

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

method of MFMailComposeViewController delegate.

Please help me out. Here is my code :

if(indexPath.section == 2)
    {

if([MFMailComposeViewController canSendMail])

{

MFMailComposeViewController *mailcontroller = [[MFMailComposeViewController alloc]init];
               [mailcontroller.mailComposeDelegate self];
               [mailcontroller setToRecipients:[[NSArray alloc]initWithObjects:record.contactemail, nil]];
               [self presentViewController:mailcontroller animated:YES completion:nil];

}

}

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

{

[self dismissViewControllerAnimated:YES completion:nil];
}
iPatel
  • 46,010
  • 16
  • 115
  • 137
Paul
  • 35
  • 1
  • 5
  • You are Cancelling the cancel button so you remain on this page (controller). Why so you required its default and correct functionality. – Arpit Kulsreshtha Mar 28 '13 at 08:54
  • So, you've tried all of them, but have you tried actually assigning yourself as your mail compose controller's delegate? – CodaFi Mar 28 '13 at 08:56
  • But i find no way to return back to my table view. i remain on the mail view only. – Paul Mar 28 '13 at 08:58
  • @Paul I don't believe you should be doing this. This changes the functionality of how apple intended for it to work and also it will make it confusing for the user. – Popeye Mar 28 '13 at 09:19
  • @CodaFI Yes i did that too. [mailcontroller.mailComposeDelegate self]; – Paul Mar 28 '13 at 09:26

1 Answers1

3

First add this two delegate in your .h file like bellow...

@interface yourViewController : UIViewController<
MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate>{
   ///your code..
}

and give delegate to self like bellow...

MFMailComposeViewController *mailcontroller = [[MFMailComposeViewController alloc]init];
mailcontroller.mailComposeDelegate = self;

and try my this method ...

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    if (result == MFMailComposeResultSent) 
    {
        NSLog(@"\n\n Email Sent");
        [AppDelegate showAlert:@"Email Sent"];

    }
    if([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
        [self dismissViewControllerAnimated:YES completion:nil];
    else
        [self dismissModalViewControllerAnimated:YES];
//    [self dismissViewControllerAnimated:YES completion:nil];
}

try this

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • @Paul see this another link related to your issue but i do above line when i want to go back from current view when i present any view.. see this http://stackoverflow.com/questions/4274895/action-sheet-doesnt-display-when-the-mfmailcomposeviewcontrollers-cancel-butto/6015957#6015957 – Paras Joshi Mar 28 '13 at 09:02
  • But i am not able to find the cancel button event as its a default button of mail view's action sheet. – Paul Mar 28 '13 at 09:31
  • @Paul you give delegate to self like this line?? mailcontroller.mailComposeDelegate = self – Paras Joshi Mar 28 '13 at 09:36
  • @Paul now i update answer try that one... – Paras Joshi Mar 28 '13 at 09:36
  • @Paul now i set whole flow related to MFMailComposeViewController , just follow these and you got the solution .. :) hope this helped you... – Paras Joshi Mar 28 '13 at 09:42
  • Thanks Paras. The problem was in setting the delegate to self. i replaced that line with the one you suggested mailcontroller.mailComposeDelegate = self. And now its working. Thanks :) – Paul Mar 28 '13 at 09:47
  • If for someone @Paras Joshi's solution didn't work, try to use instance of MFMailComposeViewController instead of self....[mailComposer dismissViewControllerAnimated:YES completion:nil]; – mavericks Jan 30 '15 at 07:16