Jea*_*ean 21 iphone cocoa-touch
我有一个简单的应用程序,它打开一个模态视图来发送电子邮件.我正在使用Xcode 4.2和iOS 5,并正在使用iOS模拟器进行测试.由于未捕获的异常'NSInvalidArgumentException',应用程序因终止应用程序崩溃
,原因是:
'应用程序试图在目标上显示一个零模态视图控制器.
执行时:
[self presentModalViewController:mailComposer animated:YES];
Run Code Online (Sandbox Code Playgroud)
虽然我已经初始化了对象'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];
}
...
...
Run Code Online (Sandbox Code Playgroud)
有什么指针吗?
Nar*_*nan 56
我也遇到过类似的问题.我分配了一个实例MFMailComposeViewController并尝试以模态方式呈现它.我也有例外:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target
这是因为在iPhone的设置中禁用了"邮件"选项.当设备中没有设置邮件帐户时也可能是这种情况.因此MFMailCompseViewController实例将以nil模态方式呈现它将导致崩溃.
我用canSendMail的方法MFMailComposeViewController来解决这个问题.
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];
}
}
Run Code Online (Sandbox Code Playgroud)
小智 16
mailComposer = [[MFMessageComposeViewController alloc] init];
Run Code Online (Sandbox Code Playgroud)
在我看来,这是问题的根源.模拟器无法发送SMS消息,因此初始化方法可能返回NULL.无论如何,苏似乎想要发送电子邮件,所以我想你需要使用
mailComposer = [[MFMailComposeViewController alloc] init];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24655 次 |
| 最近记录: |