【问题标题】:Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior不允许在解除分配时尝试加载视图控制器的视图,这可能会导致未定义的行为
【发布时间】:2015-10-16 17:23:03
【问题描述】:

我正在尝试实现发送邮件代码,但出现以下错误:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

这是我的代码片段:

    if MFMailComposeViewController.canSendMail() {
        let mail = MFMailComposeViewController()
        mail.mailComposeDelegate = self
        mail.setToRecipients(["@gmail.com"])            
        presentViewController(mail, animated: true, completion: nil)
    } else {
        // show failure alert
    }

【问题讨论】:

  • 你把这段代码放在哪里了?
  • 抱歉忘了说。它是从按钮的操作中调用的。并且类定义如下类 SettingsViewController: UITableViewController ,MFMailComposeViewControllerDelegate
  • 这不是完整的错误!请再次检查!应该说像 UIAlertController !还是什么?
  • 它的 () 。但是我的源代码中没有定义警报控制器。
  • 抱歉,您提到了哪里?

标签: ios swift mfmailcomposeviewcontroller


【解决方案1】:

如果您忘记将 segue 移除到按钮的另一个功能,则可能会发生您收到的错误。

【讨论】:

    【解决方案2】:

    我认为您的“邮件”对象超出范围并在呈现之前被释放。

    尝试在视图控制器的顶部将“邮件”设置为类级变量:

        var mail: MFMailComposeViewController?
    

    然后在您的按钮操作中设置并使用它:

        self.mail = MFMailComposeViewController()
        mail!.mailComposeDelegate = self
        //etc....
    

    编辑:邮件对象由 presentViewController 函数保留,所以我的回答不正确。感谢张勇指出这一点。

    【讨论】:

    • 据我所知,传递给presentViewController:animated:completion: 的本地定义MFMailComposeViewController 的早期版本不是问题,因为它会一直保留到视图控制器被解除。我相信还有其他未显示的代码导致了问题。
    • @DanielZhang 在这里经过一些测试后,我同意你的看法。我无法重现错误。
    • @DanielZhang 你说的很对。我刚刚意识到我忘记将 segue 删除到该按钮的其他功能。如果你把它写成答案,我会标记它。
    • @batgun 我浏览并测试了您提出的所有案例以及所有其他贡献的假设,这就是我得出这个结论的原因。很高兴你把它修好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多