尝试在取消分配时加载视图控制器的视图是不允许的,并且可能导致未定义的行为

时间:2015-10-16 17:23:04

标签: ios swift mfmailcomposeviewcontroller

我正在尝试实施发送邮件代码,但我收到以下错误:

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
    }

2 个答案:

答案 0 :(得分:2)

如果您忘记将segue删除到按钮的其他功能,则可能会收到错误。

答案 1 :(得分:1)

我认为你的“邮件”对象超出范围并在呈现之前被取消分配。

尝试在视图控制器的顶部创建“mail”类级别的var:

    var mail: MFMailComposeViewController?

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

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

编辑:currentViewController函数保留了邮件对象,所以我的回答是不正确的。感谢Daniel Zhang指出这一点。