ios - NSInvalidArgumentException',原因 : 'Application tried to present a nil modal view controller on target

标签 ios objective-c iphone xcode

我使用 Xcode 7.3 beta 3 创建了一封电子邮件。我从 appcoda 获取代码

- (IBAction)showEmail:(id)sender {
    // Email Subject
    NSString *emailTitle = @"Test Email";
    // Email Content
    NSString *messageBody = @"iOS programming is so fun!";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@"support@appcoda.com"];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipents];

    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];

}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }

    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

但在 iPhone 上运行应用程序后,它给了我一个错误(如下所示)

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target .'* First throw call stack:()libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target -- It means you try to implement the nil objcet on target check once if it is nil or not


MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
if (!mc) {  
  //When the device has not added mailViewController mail account is empty , the following present view controller causes the program to crash here
NSLog(@"no email accounts are set up in your device");  
return;  
} 

sample tutorial

关于ios - NSInvalidArgumentException',原因 : 'Application tried to present a nil modal view controller on target,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35555770/

相关文章:

iphone - Facebook iOS - handleOpenURL 和 Tabbar 应用程序

iphone - 是否可以通过短信启动 iPhone 应用程序?

iphone - iPhone 模式下 iPad 上的 Jagged Quartz 2D 渲染

ios - Facebook 与 swift 深度链接

ios - 如何删除NSUserDefaults内部的NSMutableDictionary内部的对象

objective-c - 这个宏有什么问题吗?

objective-c - 如何将 undoManager 与核心数据实体一起使用

ios - 像设置中一样选择 UITableViewCell

ios - 如何在ios 9.1中删除iPhone MDM配置?

ios - 使用 iAd 时方向更改会导致 iOS 7 上的应用程序崩溃(无法识别的选择器发送到实例)