其视图不在窗口层次结构中 - 电子邮件表单 [英] whose view is not in the window hierarchy - email form
问题描述
我在 appdelegate 中从 viewcontroller 调用一个方法.当我测试功能时,我只使用了 NSLog 消息,它工作正常(因此 viewcontroller 和 appdelegate 之间的连接是可以的).一旦我将电子邮件表单添加到此方法中,问题就会出现.我收到的消息是:
I'm calling a method from viewcontroller in an appdelegate. When I was testing functionality I just used NSLog message which works fine (so the connection between viewcontroller and appdelegate is OK). The problem appears once I add a email form into this method. The message I receive is:
Warning: Attempt to present <MFMailComposeViewController: 0x1fdc3990> on <ViewController: 0x1fd9e3b0> whose view is not in the window hierarchy!
谁知道该怎么做?我知道有很多主题涉及其视图不在窗口层次结构中"的问题,但没有一个对我有帮助.
Anyone who know what to do? I know there are lot of topics with 'whose view is not in the window hierarchy' issue but none of them helped me.
ViewController.m
ViewController.m
...
-(void)mail{
NSLog(@"blablabla");
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:@"Hello and welcome!"];
NSArray *toRecipients = [NSArray arrayWithObject:@"tomas.javnicky@gmail.com"];
[mail setToRecipients:toRecipients];
[mail setCcRecipients:toRecipients];
NSString *emailBody = @"Hey all!";
[mail setMessageBody:emailBody isHTML:NO];
mail.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentViewController:mail animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
message:@"E-mail is not supported on your device"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result) {
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
NSLog(@"mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"mail failed");
break;
}
[self dismissViewControllerAnimated:YES
completion:nil];
}
...
Appdelegate.m
Appdelegate.m
...
-(void)something {
ViewController * vc = [[ViewController alloc] init];
[vc mail];
}
...
这就是解决我的问题的方法:
This is what solved my problem:
- (void)something {
ViewController *rootViewController = (ViewController*)self.window.rootViewController;
[rootViewController mail];
}
另请查看 rmaddy 的答案以获取更多信息.
Also check the answer by rmaddy for more info.
推荐答案
既然您已经发布了代码,问题就很明显了.您创建了一个视图控制器,但从未显示它(在 something
方法中).然后你在这个未显示的视图控制器 (mail
) 上调用一个方法,它试图从未显示的视图控制器显示邮件视图控制器.这就是导致错误的原因.
Now that you've posted code, the problem is obvious. You create a view controller but you never display it (in the something
method). Then you call a method on this undisplayed view controller (mail
) which attempts to display the mail view controller from the undisplayed view controller. This is what causes the error.
您需要从已经显示的视图控制器中显示邮件控制器(例如 rootViewController
可能).
You need to display the mail controller from a view controller that is already being displayed (such as the rootViewController
maybe).
你的 ViewController
类的重点是什么?
What is the point of your ViewController
class?
这篇关于其视图不在窗口层次结构中 - 电子邮件表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!