2016-02-04 170 views
0

我试图让我的应用程序能够在按下按钮后访问并发送电子邮件。这是我到目前为止,当我运行它,并单击帮助按钮弹出警报和取消按钮工作正常,但警报的电子邮件部分崩溃应用程序。 当它崩溃时,突出显示class AppDelegate: UIResponder, UIApplicationDelegate行,并说Thread 1: signal SIGABRT使用MFMail从应用程序发送电子邮件

@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 = ["[email protected]"] 
     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 

回答

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) 

Instantiate and Present a viewController in Swift

相关问题