youshunei youshunei - 5 months ago 15
Objective-C Question

MFMailComposeViewController navigation bar buttons are disabled

I use MFMailComposeViewController to send mail in my app. But when present mail compose view controller, all of navigation buttons are disabled (except back button in select mail address screen), i must use Home button to quit app. Does anyone has idea?
Here is screen shot:
Screen shot
2



Code:




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


Delegate method :




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


And in header file, I declared implement MFMailComposeViewControllerDelegate.

Answer

I had exactly the same problem. and it took me a while to figure this out but no surprise it came down to customized UIBarButtonItem

I bet in your UIBarButtonItem.h there is a method

-(void)setEnabled:(BOOL)enabled ;

and the implementation looks like this:

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

and this is causing problem so as soon as you comment out this method your problem should go away.

Comments