其视图不在窗口层次结构中-电子邮件表单 [英] 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
类的重点是什么?
这篇关于其视图不在窗口层次结构中-电子邮件表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!