全局创建显示警报功能并从任何视图控制器调用它
create a display alert function globally and call it from any view controller
func displayalert(title:String, message:String, vc:UIViewController)
{
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
self.dismiss(animated: true, completion: nil)
})))
vc.present(alert, animated: true, completion: nil)
}
this is the function i have used.i tried to call it like this,
displayalert1(title:"dsfvasdcs", message:"easfSDXCSDZX", vc:validateOTPViewController())
返回错误 "BAD ACCESS"。 vc.present 是 运行 就像一个循环。我不明白问题出在哪里。
您正在将 validateOTPViewController
的新实例传递给 displayalert
函数。
改为:
displayalert1(title:"dsfvasdcs", message:"easfSDXCSDZX", vc:self)
这会将当前视图控制器传递给函数,而不是传递一个尚未呈现的新视图控制器。
我 运行 你的代码,它工作正常。我认为你会在 vc 中传递 self。
self.displayalert(title: "Title", message: "Some Message", vc: self)
您还可以对 UIViewController 进行扩展-
extension UIViewController {
// Your Function...
}
现在您可以从任何视图控制器全局访问此功能,只需键入-
self.displayalert(title: "Title", message: "Some Message", vc: self)
Swift 4
使用您的函数创建 UIViewController
的扩展以显示带有必需参数参数的警报
extension UIViewController {
func displayalert(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
alert.dismiss(animated: true, completion: nil)
})))
self.present(alert, animated: true, completion: nil)
}
}
现在从你的视图控制器调用这个函数:
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.displayalert(title: <String>, message: <String>)
}
}
func displayalert(title:String, message:String, vc:UIViewController)
{
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
self.dismiss(animated: true, completion: nil)
})))
vc.present(alert, animated: true, completion: nil)
}
this is the function i have used.i tried to call it like this,
displayalert1(title:"dsfvasdcs", message:"easfSDXCSDZX", vc:validateOTPViewController())
返回错误 "BAD ACCESS"。 vc.present 是 运行 就像一个循环。我不明白问题出在哪里。
您正在将 validateOTPViewController
的新实例传递给 displayalert
函数。
改为:
displayalert1(title:"dsfvasdcs", message:"easfSDXCSDZX", vc:self)
这会将当前视图控制器传递给函数,而不是传递一个尚未呈现的新视图控制器。
我 运行 你的代码,它工作正常。我认为你会在 vc 中传递 self。
self.displayalert(title: "Title", message: "Some Message", vc: self)
您还可以对 UIViewController 进行扩展-
extension UIViewController {
// Your Function...
}
现在您可以从任何视图控制器全局访问此功能,只需键入-
self.displayalert(title: "Title", message: "Some Message", vc: self)
Swift 4
使用您的函数创建 UIViewController
的扩展以显示带有必需参数参数的警报
extension UIViewController {
func displayalert(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
alert.dismiss(animated: true, completion: nil)
})))
self.present(alert, animated: true, completion: nil)
}
}
现在从你的视图控制器调用这个函数:
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.displayalert(title: <String>, message: <String>)
}
}