2016-02-04 162 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

相關問題