【问题标题】:UIAlertView' was deprecated in iOS 9.0UIAlertView' 在 iOS 9.0 中已弃用
【发布时间】:2016-01-14 00:05:43
【问题描述】:

我尝试了几种方法来使用 UIAlertController,而不是 UIAlertView,但我无法发出警报。提前感谢您提供任何进一步的建议。我是新手。

UIAlertView' 在 iOS 9.0 中已弃用:UIAlertView 已弃用。将 UIAlertController 与 UIAlertControllerStyleAlert 的首选样式一起使用

这是我的代码:

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)
    }
}

【问题讨论】:

  • 什么不起作用?我看到了您的UIAlertView 代码,但没有看到您对UIAlertController 的尝试。周围有很多教程。 Here 就是一个例子。
  • 更新您的问题,尝试使用UIAlertController。解释你遇到了什么问题。并进行一些搜索。使用UIAlertController的例子数不胜数。

标签: ios uialertview ios9 deprecated


【解决方案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)

【讨论】:

    【解决方案2】:

    试试这样的...

    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)
    

    【讨论】:

      【解决方案3】:
          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 初始化后将操作添加到警报控制器。 UIAlertCOntroller 是 UIViewController 的子类。所以你可以使用 self.presentViewController 方法在当前控制器上显示警报。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-31
        • 1970-01-01
        • 1970-01-01
        • 2015-09-15
        • 2016-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多