我尝试呈现邮件格式时出现错误?

时间:2016-02-11 02:34:37

标签: swift mfmessagecomposeview

我尝试在紧急课程的主菜单课程中提供消息格式,但它没有提供任何内容,这就是Xcode不断告诉我的内容

Warning: Attempt to present <MFMessageComposeViewController: 0x13703dc00> on <Emergency: 0x1365a4b60> whose view is not in the window hierarchy!
Warning: Attempt to present <MFMessageComposeViewController: 0x13703dc00>  on <MainMenu: 0x136649fd0> which is already presenting (null)

这里是代码

MainMenu类

class MainMenu: UIViewController{

    let getHelp:Emergency = Emergency()
    @IBAction func TapOnEmergency(sender: UIButton) {


         getHelp.emergencyMessage()

        getHelp.presentViewController(getHelp.messageComposer,animated: false,completion: nil)

        dispatch_async(dispatch_get_main_queue(), {
            self.presentViewController(self.getHelp.messageComposer, animated: true, completion: nil)
        })
    }
    }

紧急课程

class Emergency: UIViewController,MFMessageComposeViewControllerDelegate{

    func emergencyMessage()
    {

         let textMessageRecipients = [number!]



                if (MFMessageComposeViewController.canSendText() ) {
                    messageComposer.recipients = textMessageRecipients
                    messageComposer.body = ""
                    messageComposer.messageComposeDelegate = self
                   presentViewController(messageComposer,animated: false,completion: nil)

        } else {

    let errorAlert = UIAlertController(title: "Cannot Send Text Message", message: "Your device is not able to send text messages.", preferredStyle: UIAlertControllerStyle.Alert)

    errorAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil))

    self.presentViewController(errorAlert, animated: true, completion: nil)

        }


    }

我如何解决这个问题?

请注意,紧急情况没有场景/视图

1 个答案:

答案 0 :(得分:1)

紧急类不应继承UIViewController,因为它不是视图控制器,也不管理视图。但是,它需要能够呈现视图控制器本身,因此需要将其传递给一个。因此,您的MainMenu变为:

class MainMenu: UIViewController {
    let getHelp:Emergency = Emergency()
    @IBAction func TapOnEmergency(sender: UIButton) {
        getHelp.emergencyMessage(self)
    }
}

紧急课程成为:

class Emergency: NSObject, MFMessageComposeViewControllerDelegate {
    func emergencyMessage(vc: UIViewController)
    {
         let textMessageRecipients = [number!]
         if (MFMessageComposeViewController.canSendText() ) {
             messageComposer.recipients = textMessageRecipients
             messageComposer.body = ""
             messageComposer.messageComposeDelegate = self
             vc.presentViewController(messageComposer, animated: false,completion: nil)
         } else {
             let errorAlert = UIAlertController(title: "Cannot Send Text Message", message: "Your device is not able to send text messages.",                 preferredStyle: UIAlertControllerStyle.Alert)
             errorAlert.addAction(UIAlertAction(title: "OK", style:  UIAlertActionStyle.Cancel, handler: nil))
            vc.presentViewController(errorAlert, animated: true, completion: nil)
         }
    }
}