2015-10-15 53 views
-4

我已經嘗試了幾種使用UIAlertController而不是UIAlertView的方式,但我無法使警報消失。預先感謝您提出任何進一步的建議。我是一個新手。UIAlertView'已被棄用在iOS 9.0

UIAlertView'在iOS 9.0中已棄用:UIAlertView已棄用。使用UIAlertController與UIAlertControllerStyleAlert的preferredStyle代替

這裏是我的代碼:

import MessageUI 

class SecondViewController: UIViewController,MFMailComposeViewControllerDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    @IBAction func sendmail(sender: UIButton) { 
      let mailComposeViewController = configuredMailComposeViewController() 
     if MFMailComposeViewController.canSendMail() { 
      self.presentViewController(mailComposeViewController, animated: true, completion: nil) 
     } else { 
      self.showSendMailErrorAlert() 
     } 
    } 

    func configuredMailComposeViewController() -> MFMailComposeViewController { 
     let mailComposerVC = MFMailComposeViewController() 
     mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property 

     mailComposerVC.setToRecipients(["[email protected]"]) 
     mailComposerVC.setSubject("App Feedback") 
     mailComposerVC.setMessageBody("Feature request or bug report?", isHTML: false) 

     return mailComposerVC 
    } 

    func showSendMailErrorAlert() { 
     let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK") 
     sendMailErrorAlert.show() 
    } 

    // MARK: MFMailComposeViewControllerDelegate Method 
    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
     controller.dismissViewControllerAnimated(true, completion: nil) 
    } 
} 
+0

什麼不行?我看到你的'UIAlertView'代碼,但不是你在'UIAlertController'上的嘗試。有很多教程。 [Here](http://nshipster.com/uialertcontroller/)就是一個例子。 –

+1

嘗試使用'UIAlertController'來更新您的問題。解釋你有什麼問題。並做一些搜索。有無數如何使用'UIAlertController'的例子。 – rmaddy

回答

1

使用UIAlertController

這裏一個例子:

//Create the AlertController 
    let actionSheetController: UIAlertController = UIAlertController(title: "Alert", message: "Swiftly Now! Choose an option!", preferredStyle: .Alert) 

    //Create and add the Cancel action 
    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in 
    //Do some stuff 
    } 
    actionSheetController.addAction(cancelAction) 
    //Create and an option action 
    let nextAction: UIAlertAction = UIAlertAction(title: "Next", style: .Default) { action -> Void in 
    //Do some other stuff 
    } 
    actionSheetController.addAction(nextAction) 
    //Add a text field 
    actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in 
     //TextField configuration 
    textField.textColor = UIColor.blueColor() 
    } 

    //Present the AlertController 
    self.presentViewController(actionSheetController, animated: true, completion: nil) 
0

嘗試這樣的事情...

let sendMailErrorAlert = UIAlertController(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", preferredStyle: .Alert) 

self.presentViewController(sendMailErrorAlert, animated: true, completion: nil) 
0
let alertController = UIAlertController(title: "Could Not Send Email 
    ", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", preferredStyle: .Alert) 

使用UIAlertController 初始化添加操作給alertcontroller後。 UIAlertCntroller是UIViewController的子類。所以你可以在當前控制器上使用self.presentViewController方法show alert。

相關問題