开发者

MFMailComposeViewController in the delegate

开发者 https://www.devze.com 2023-04-09 02:02 出处:网络
The question deals with an application which uses many views in a UINavigation controller Style. I have a simple function in my delegate which can be used by all views to plot-out error message

The question deals with an application which uses many views in a UINavigation controller Style.

I have a simple function in my delegate which can be used by all views to plot-out error message

// In Appdelegate.m

-(开发者_运维知识库void)popErrorWindow:(NSString *)theError
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:theError
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Report",nil];
    [alert show];
    [alert release];
}


- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex == 1)
        {
            NSLog(@"report");
            [self mailIt:@"error name"];
        }
    }

Now, wanting to have a mechanism that will email the error along with some other data I have created this:

-(void)mailIt:(NSString *)theError {
    NSLog(@"Mail it");
    pickerMail = [[MFMailComposeViewController alloc] init];
    pickerMail.mailComposeDelegate = self;

    [pickerMail setSubject:@"error via email"];

NSMutableString *body = [NSMutableString string];

    [body appendString:@"Error XXX "];

    [pickerMail setMessageBody:body isHTML:YES];


    // Problem here:
    [self.window presentModalViewController:pickerMail animated:YES];   
}

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

{
    // Problem here:
    [self.window  dismissModalViewControllerAnimated:YES];
    //NSLog(@"mail was sent");
}

The problem is in self.window , which is not the right way to access this from the delegate, I still want to have the mail element in the delegate as all views can call the error alert, and I would like to have only one place for this mechanism.

How should I do it from inside the delegate, what should replace the self.window?


Perhaps reimplementing popErrorWindow: and mailIt: in a category on UIViewController. This way you have access to the top-level view controller to call presentModalViewController and dismissModalViewControllerAnimated on.

Alternatively you can do this in a subclass of UIViewController and then make your other custom view controller's subclass of that. The downside to this method is when you have subclasses of classes other than UIViewController


- (void)mailComposeController:(MFMailComposeViewController *)controller 
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error
{
    [controller dismissModalViewControllerAnimated:YES];
}

EDIT :

The - (void)presentModalViewController:(UIViewController *)vc and - (void)dismissModalViewControllerAnimated:(BOOL)animated methods are an UIViewController instance method, so you cannot use it with an UIWindow.

In order to present your mail controller with a nice animation, you can do that :

UIViewController *aController = self.navigationController.presentedViewController;
[aController presentModalViewController:pickerMail animated:YES];
0

精彩评论

暂无评论...
验证码 换一张
取 消