MFMailComposeViewController导航栏按钮被禁用

时间:2012-01-14 07:04:40

标签: iphone objective-c button sendmail

我使用MFMailComposeViewController在我的应用中发送邮件。但是当存在邮件撰写视图控制器时,所有导航按钮都被禁用(选择邮件地址屏幕中的后退按钮除外),我必须使用主页按钮退出应用程序。有没有人有想法? 这是屏幕截图: Screen shot 2


码:

- (void)shareVieEmail
{
    if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
        mailViewController.mailComposeDelegate = self;
        [mailViewController setSubject:@"Test subject"];
        [mailViewController setMessageBody:@"Mail message body" isHTML:NO];

        NSData *imageData = [NSData dataWithContentsOfFile:photourl];
        [mailViewController addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"example_photo"];
        [self presentModalViewController:mailViewController animated:YES];
    } else {
        [[[UIAlertView alloc] initWithTitle:@"Cannot send mail" message:@"Device is unable to send email in its current state" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
    }
}

委托方法:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            //NSLog(@"Result: canceled");
            break;
        case MFMailComposeResultSaved:
            //NSLog(@"Result: saved");
            break;
        case MFMailComposeResultSent:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result" message:@"Mail Sent Successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
            break;
        case MFMailComposeResultFailed:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result" message:@"Mail Sent Failed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
            break;
        default:
            //NSLog(@"Result: not sent");
            break;
    }
    if (error) {
        [[[UIAlertView alloc] initWithTitle:@"Cannot send mail" message:[NSString stringWithFormat:@"ERROR:%@", [error userInfo]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
    }
    [self dismissModalViewControllerAnimated:YES];
}

在头文件中,我声明了实现MFMailComposeViewControllerDelegate。

3 个答案:

答案 0 :(得分:2)

我有完全相同的问题。我花了一段时间才弄清楚这一点,但毫不奇怪,它归结为定制的UIBarButtonItem

我打赌你的UIBarButtonItem.h有一个方法

-(void)setEnabled:(BOOL)enabled ;

,实现如下:

-(void)setEnabled:(BOOL)enabled {
    if (self.customView) {
        if ([[self.customView.subviews objectAtIndex:0] isKindOfClass:[UIButton class]])         {
            ((UIButton*)[self.customView.subviews objectAtIndex:0]).enabled = enabled;
        }
    }
}

这会导致问题,因此只要您注释掉此方法,您的问题就会消失。

答案 1 :(得分:0)

在MFMailComposeViewController的委托中,您需要实现didFinishWithResult:并从那里解除模态视图控制器。

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{ 
    // you can test the result of the mail sending here if you want

    [self dismissModalViewControllerAnimated:YES];
}

适用于swift 4.0 +

func mailComposeController(controller: MFMailComposeViewController,
                           didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    // Check the result or perform other tasks.

    // Dismiss the mail compose view controller.
    controller.dismissViewControllerAnimated(true, completion: nil)
}

答案 2 :(得分:0)

我也遇到了这个问题,但我的情况是因为我已经setNavigationBarHidden:animated:覆盖了UINavigationController CNContactViewController MFMailComposeViewController,因为topViewController中存在错误。[sippuac] type=friend username=sippuac host=127.0.0.1 port=5061 context=test dtmfmode=rfc2833 insecure=very canreinvite=no nat=yes [sippuas] type=friend username=sippuas host=127.0.0.1 port=5062 context=test dtmfmode=rfc2833 insecure=very canreinvite=no nat=yes 。一个仍然包含变通方法并在[test] exten=>s,1,Dial(SIP/sippuas,20) 中解决问题的解决方案是使用方法调配来调用原始方法或被覆盖的方法,具体取决于当前sipp -sn uas -rsa 127.0.0.1:5060 -p 5062 -i 127.0.0.1 -mp 6001 sipp -sn uac 127.0.0.1:5060 -s s -p 5061 -i 127.0.0.1 的类