当我尝试呈现消息格式时出现错误?
error appears when I try to present message format?
我尝试在紧急情况 class 中在主菜单 class 中显示消息格式,但它没有显示任何内容,这是 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)
这是代码
主菜单class
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
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)
}
}
我该如何解决这个问题?
请注意,紧急情况没有 scene/view
Emergency class 不应继承自 UIViewController
,因为它不是视图控制器,也不管理视图。但是,它需要能够呈现一个视图控制器本身,因此需要传入一个。因此您的 MainMenu 变为:
class MainMenu: UIViewController {
let getHelp:Emergency = Emergency()
@IBAction func TapOnEmergency(sender: UIButton) {
getHelp.emergencyMessage(self)
}
}
紧急情况 class 变为:
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)
}
}
}
我尝试在紧急情况 class 中在主菜单 class 中显示消息格式,但它没有显示任何内容,这是 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)
这是代码
主菜单class
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
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)
}
}
我该如何解决这个问题?
请注意,紧急情况没有 scene/view
Emergency class 不应继承自 UIViewController
,因为它不是视图控制器,也不管理视图。但是,它需要能够呈现一个视图控制器本身,因此需要传入一个。因此您的 MainMenu 变为:
class MainMenu: UIViewController {
let getHelp:Emergency = Emergency()
@IBAction func TapOnEmergency(sender: UIButton) {
getHelp.emergencyMessage(self)
}
}
紧急情况 class 变为:
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)
}
}
}