[英]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.