Swift UIAlertController显示自定义消息

时间:2016-06-14 16:40:05

标签: ios swift uialertcontroller

我有一个ViewController类,其唯一目的是显示一条警报消息,其中包含通过自定义初始化消息传递的自定义消息和标题。一旦视图出现,就在viewDidLoad中完成。然而我的问题是,当涉及到这个视图时,它会弹出并永远停留在这个视图中,而不是仅仅将视图放在另一个视图上。我不知道如何解决这个问题。这是我的alertVC类的代码

import UIKit

class AlertVC: UIViewController {

  var myMessage: String?
  var myTitle: String?

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }


  override func viewDidAppear(animated: Bool){
    let alertController = UIAlertController(title: myTitle, message: myMessage, preferredStyle: .Alert)
    let OKAction = UIAlertAction(title: "OK", style: .Default) {
        (action: UIAlertAction) in print("Youve pressed OK Button")
    }
    alertController.addAction(OKAction)
    self.presentViewController(alertController, animated: true, completion: nil)
  }

  convenience init(title: String, message: String){
    self.init()
    self.myTitle = title
    self.myMessage = message
  }
}

这是我为此创建对象的代码,并尝试显示它。

let alert = AlertVC(title: "Error", message: "error")
presentViewController(alert, animated: true, completion: nil)

感谢任何帮助,如果您需要更多信息,请发表评论。谢谢!

3 个答案:

答案 0 :(得分:18)

为什么不简单地extension

extension UIViewController {

  func presentAlert(withTitle title: String, message : String) {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
    let OKAction = UIAlertAction(title: "OK", style: .default) { action in
        print("You've pressed OK Button")
    }
    alertController.addAction(OKAction)
    self.present(alertController, animated: true, completion: nil)
  }
}

并用

调用它
presentAlert(withTitle: "Error", message: "error")

在任何UIViewController

答案 1 :(得分:1)

如果AlertVC的唯一目的是显示UIAlertController,为什么不在您之前的UIAlertController上展示ViewController

所以你写的是:

let alert = AlertVC(title: "Error", message: "error")
presentViewController(alert, animated: true, completion: nil)

将其替换为:

let alertController = UIAlertController(title: myTitle, message: myMessage, preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
    (action: UIAlertAction) in print("Youve pressed OK Button")
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)

编辑:阅读完评论后,我了解您要避免多次输入相同的代码。我做了一些快速的研究,看起来你shouldn't subclass UIAlertController,所以也许扩展可能有用吗?类似的东西:

extension UIAlertController{
    class func customAlertController(title : String, message : String) -> UIAlertController{
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let OKAction = UIAlertAction(title: "OK", style: .Default) {
            (action: UIAlertAction) in print("Youve pressed OK Button")
        }
        alertController.addAction(OKAction)
        return alertController
    }
}

然后替换:

let alert = AlertVC(title: "Error", message: "error")
presentViewController(alert, animated: true, completion: nil)

使用:

    let alert = UIAlertController.customAlertController("Error!", message: "error")
    self.presentViewController(alert, animated: true) {

    }

答案 2 :(得分:0)

@ vadian在Swift 3中的回答,并将完成处理程序添加到警报控制器解除。

extension UIViewController {

    func presentAlertWithTitle(title: String, message : String, onDismiss: SimpleCompletionBlock? = nil)
    {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

        let OKAction = UIAlertAction(title: "OK", style: .Default) {
            (action: UIAlertAction) in print("Youve pressed OK Button")

            onDismiss?()
        }

        alertController.addAction(OKAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}