【问题标题】:NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on targetNSInvalidArgumentException',原因:'应用程序试图在目标上呈现一个 nil 模态视图控制器
【发布时间】:2016-06-04 00:07:15
【问题描述】:

我使用 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 上运行应用程序后,它给了我一个错误(如下所示)

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图在目标上呈现 nil 模态视图控制器。”* 首先抛出调用堆栈:()libc++abi。 dylib:以 NSException 类型的未捕获异常终止

【问题讨论】:

  • 当您收到此错误时,例如,如果您按下 showEmail:(id)sender 这个或其他地方的按钮
  • 我不太明白,你能解释一下吗?
  • 当你收到错误Application tried to present a nil modal view controller on target

标签: ios objective-c iphone xcode


【解决方案1】:

NSInvalidArgumentException',原因:'应用程序试图在目标上呈现一个 nil 模态视图控制器——这意味着你尝试在目标上执行一次 nil objcet 检查它是否为 nil

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;  
} 

样本tutorial

【讨论】:

    【解决方案2】:

    斯威夫特

    如果让则使用

    if let mc: MFMailComposeViewController = MFMailComposeViewController() {
        mc.mailComposeDelegate = self
        mc.setSubject(emailTitle)
        mc.setToRecipients(toRecipents)
        self.presentViewController(mc, animated: true, completion: nil)
    }
    

    【讨论】:

    • 使用相关的MFMessageComposeViewController,我也明白了。 (Xcode 10,斯威夫特 4.2)。令人困惑的是,当您创建 VC 时,如果您等待的时间足够长(您几乎必须注释掉 present 行,MF 将进行服务检测并显示自己的警报:“无法发送消息”。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 2016-12-22
    相关资源
    最近更新 更多