2011-09-29 72 views
0

该问题涉及在UINavigation控制器样式中使用许多视图的应用程序。代理中的MFMailComposeViewController

我有我委托一个简单的功能,可以通过所有视图可用于绘制出错误信息

//在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,这是不是从委托访问的正确方法, 我仍然希望在委托中具有邮件元素,因为所有视图都可以调用错误警报,并且我希望只有一个位置用于此机制。

我应该怎样从委托内部做些什么来取代self.window?

回答

1
- (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]; 
+0

那么:[self.window presentModalViewController:pickerMail animated:YES]; – chewy

+0

我已编辑我的回答 – klefevre

+0

致谢至此kl94, 加入:[self.navigationController.parentViewController presentViewController:pickerMail]; 我得到的错误: 语义问题:方法'-presentViewController:'未找到(返回类型默认为'id') – chewy

2

UIViewController也许重新实现popErrorWindow:mailIt:category。通过这种方式,您可以访问顶级视图控制器,以打开presentModalViewControllerdismissModalViewControllerAnimated

或者,您可以在UIViewController的子类中执行此操作,然后制作您的其他自定义视图控制器的子类。这种方法的缺点是当你有其他类的子类除了UIViewController

相关问题