尝试PresentModalViewController时获取异常

时间:2011-02-01 08:48:43

标签: iphone-sdk-3.0

我正在我的应用程序中实现MFMailComposeViewController。当我尝试使用以下代码打开邮件编辑器时,我遇到异常,例如“由于未捕获的异常终止应用程序'NSInvalidArgumentException',原因:'应用程序试图在目标上显示nil模态视图控制器”。我不知道我犯了什么错。你们可以看看下面的代码并建议我。

   [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil ];

MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate =self ;
controller.delegate=self;
controller.title=@"Title";
[controller setSubject:@"See my Notes"];
NSArray* toRecipients=nil;  

[controller setToRecipients:toRecipients];

NSString *emailBody = @"";
emailBody = @"<html><head><title><style type=\"text/css\">a {color: red}</style>";
emailBody = [emailBody stringByAppendingString:@"</title></head><body style='background-color: transparent;color:black'> "];
emailBody = [emailBody stringByAppendingString:@"Hello all"];
emailBody = [emailBody stringByAppendingString:@"</body></html>"];
[controller setMessageBody:emailBody isHTML:YES];

[self presentModalViewController:controller animated:YES];

提前致谢, -Sek。

1 个答案:

答案 0 :(得分:1)

嗯......我在没有配置电子邮件客户端的设备上发送电子邮件时遇到过类似的问题/崩溃(但在模拟器中不是问题)。

你可以测试这样的情况......

// viewWillAppear

if (![MFMailComposeViewController canSendMail]) sendEmailButton.enabled = NO;