我正在尝试让我的应用能够在按下按钮后访问并发送电子邮件。这是我到目前为止,当我运行它,并单击帮助按钮弹出警报,取消按钮工作正常,但警报的电子邮件部分崩溃应用程序。
当它崩溃时,会突出显示class AppDelegate: UIResponder, UIApplicationDelegate
行并显示Thread 1: signal SIGABRT
。
@IBAction func helpButtonAction(sender: UIButton) {
let alert = UIAlertController(title: "Help", message: "Click 'Email' to email support", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Email", style: UIAlertActionStyle.Default, handler: { action in
let emailTitle = "Help Request"
let messageBody = ""
let toRecipents = ["sample@sample.com"]
let mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
self.dismissViewControllerAnimated(false, completion: nil)
}
继承人控制台错误......
'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <app.ViewController: 0x13c64a5f0>.'
*** First throw call stack:
(0x183930f48 0x19847ff80 0x18923def4 0x189240800 0x188fbdea0 0x10004909c 0x1000491d0 0x100048cbc 0x100048d0c 0x1893297d8 0x189329f70 0x189218ba4 0x18921bd9c 0x188ff3668 0x188eff240 0x188efed3c 0x188efebc4 0x1886c5c2c 0x1013d5c68 0x1013db710 0x1838e81f8 0x1838e6060 0x183814ca0 0x18e87c088 0x188f2cffc 0x10004ee78 0x198cc28b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
答案 0 :(得分:1)
问题是如何实例化视图控制器。
此代码将实例化类,但不是您想要的视图。
let mc: MFMailComposeViewController = MFMailComposeViewController()
正确的方法可以在上面的链接中找到,我将在这里复制代码。
您必须为视图控制器设置一个标识符,通过故事板对其进行实例化,然后呈现它。
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)