UIAlertView'在iOS 9.0中已被弃用

时间:2015-10-15 13:37:48

标签: ios uialertview ios9 deprecated

我已经尝试了几种使用UIAlertController的方法,而不是UIAlertView,但我无法提醒。提前感谢任何进一步的建议。我是新手。

在iOS 9.0中不推荐使用

UIAlertView:不推荐使用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(["blabla@gmail.com"])
        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)
    }
}

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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)

答案 2 :(得分: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添加操作。 UIAlertCOntroller是UIViewController的子类。因此,您可以在当前控制器上使用self.presentViewController方法show alert。