gpt4 book ai didi

iphone - 尝试打开邮件编辑器时,“应用程序试图在目标上呈现零模态视图 Controller ”错误/崩溃

转载 作者:行者123 更新时间:2023-12-03 18:18:34 25 4
gpt4 key购买 nike

我有一个简单的应用程序,它打开一个模态视图来发送电子邮件。我正在使用 Xcode 4.2 和 iOS 5,并使用 iOS 模拟器进行测试。该应用程序崩溃了 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:
'应用程序试图在目标上呈现一个零模态视图 Controller .'

执行该行时:

 [self presentModalViewController:mailComposer animated:YES];

虽然我已经初始化了对象“mailComposer”。

com_FirstViewController.m 类:

#import "com_FirstViewController.h"
...
@implementation com_FirstViewController
....
....
-(void)showEmailComposer {

Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
if ([mailClass canSendMail]) {
NSLog(@"showEmailComposer: Calling displayComposerSheet");
[self displayComposerSheet];

} else {
NSLog(@"showEmailComposer: Calling launchMailAppOnDevice");
[self launchMailAppOnDevice];
}
}
else {
NSLog(@"showEmailComposer: Calling launchMailAppOnDevice");
[self launchMailAppOnDevice];
}
}



#pragma mark -
#pragma mark Compose Mail

-(void) displayComposerSheet {

mailComposer = [[MFMessageComposeViewController alloc] init];
mailComposer.messageComposeDelegate = self;

// Set the mail title
[mailComposer setTitle:@"Mail Title"];

// Set the recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"user@company.com"];

[mailComposer setRecipients:toRecipients];

// EMail Body
NSString *mailBody = @"This is the mail body";
[mailComposer setBody:mailBody];

NSLog(@"present the modal view ctlr");
[self presentModalViewController:mailComposer animated:YES];
}
...
...

请问有什么指点吗?

最佳答案

我也遇到过类似的问题。我分配了一个 MFMailComposeViewController 实例并尝试以模态方式呈现它。我也遇到了一个异常(exception):

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“应用程序尝试在目标上呈现 nil 模态视图 Controller

这是因为 iPhone 设置中禁用了“邮件”选项。当设备中没有设置邮件帐户时也可能出现这种情况。因此,MFMailCompseViewController 实例将为 nil,并且以模态方式呈现它会导致崩溃。

我使用 MFMailComposeViewControllercanSendMail 方法来解决此问题。

Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil) {
MFMailComposeViewController * mailView = [[MFMailComposeViewController alloc] init];
mailView.mailComposeDelegate = self;

//Set the subject
[mailView setSubject:emailSubject];

//Set the mail body
[mailView setMessageBody:emailBody isHTML:YES];


//Display Email Composer
if([mailClass canSendMail]) {
[self.navControl presentModalViewController:mailView animated:YES];
}
}

关于iphone - 尝试打开邮件编辑器时,“应用程序试图在目标上呈现零模态视图 Controller ”错误/崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10435496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com