2011-09-29 43 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

相關問題