Swift 显示自定义消息的 UIAlertController
Swift UIAlertController showing custom message
我有一个 ViewController class,其唯一目的是显示一条带有自定义消息和标题的警报消息,该消息通过自定义初始化消息传入。一旦视图出现,就会在 viewDidLoad 中完成此操作。然而,我的问题是,当涉及到这个视图时,它会弹出并永远停留在这个视图中,而不是仅仅将视图放在另一个视图之上。我不确定如何解决这个问题。这是我的 alertVC class
的代码
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
}
}
这是我如何为此创建 object 并尝试展示它的代码。
let alert = AlertVC(title: "Error", message: "error")
presentViewController(alert, animated: true, completion: nil)
感谢任何帮助,如果您需要更多信息,请发表评论。谢谢!
如果 AlertVC
的唯一目的是显示 UIAlertController
,你为什么不在你之前的 ViewController
上显示 UIAlertController
?
所以你在哪里写的:
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) {
}
为什么不直接 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
class
@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)
}
}
我有一个 ViewController class,其唯一目的是显示一条带有自定义消息和标题的警报消息,该消息通过自定义初始化消息传入。一旦视图出现,就会在 viewDidLoad 中完成此操作。然而,我的问题是,当涉及到这个视图时,它会弹出并永远停留在这个视图中,而不是仅仅将视图放在另一个视图之上。我不确定如何解决这个问题。这是我的 alertVC class
的代码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
}
}
这是我如何为此创建 object 并尝试展示它的代码。
let alert = AlertVC(title: "Error", message: "error")
presentViewController(alert, animated: true, completion: nil)
感谢任何帮助,如果您需要更多信息,请发表评论。谢谢!
如果 AlertVC
的唯一目的是显示 UIAlertController
,你为什么不在你之前的 ViewController
上显示 UIAlertController
?
所以你在哪里写的:
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) {
}
为什么不直接 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
class
@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)
}
}