[英]MFMailComposeViewController in the delegate
该问题涉及一个应用程序,该应用程序使用UINavigation控制器样式中的许多视图。
我的委托人中有一个简单的函数,所有视图都可以使用该函数来绘制错误消息
//在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"];
}
}
现在,想要一种将错误信息连同其他一些数据一起通过电子邮件发送的机制,我已经创建了:
-(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");
}
问题出在self.window中,这不是从委托访问此方法的正确方法,我仍然希望委托中包含mail元素,因为所有视图都可以调用错误警报,并且我只想拥有一个地方对于这种机制。
我应该如何从委托内部执行此操作,应该用什么替换self.window?
也许在UIViewController
的类别中重新实现popErrorWindow:
和mailIt:
这样,您可以访问顶级视图控制器以调用presentModalViewController
和dismissModalViewControllerAnimated
。
另外,您可以在UIViewController
的子类中执行此操作,然后将其作为其他自定义视图控制器的子类。 该方法的缺点是当您具有UIViewController
以外的其他类的子类时
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
[controller dismissModalViewControllerAnimated:YES];
}
编辑:
- (void)presentModalViewController:(UIViewController *)vc
和- (void)dismissModalViewControllerAnimated:(BOOL)animated
方法是UIViewController
实例方法,因此您不能将其与UIWindow
一起使用。
为了向您的邮件控制器呈现漂亮的动画,您可以执行以下操作:
UIViewController *aController = self.navigationController.presentedViewController;
[aController presentModalViewController:pickerMail animated:YES];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.