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?
EDIT :
The
- (void)presentModalViewController:(UIViewController *)vcand- (void)dismissModalViewControllerAnimated:(BOOL)animatedmethods are anUIViewControllerinstance method, so you cannot use it with anUIWindow.In order to present your mail controller with a nice animation, you can do that :