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像循环一样运行。我不明白问题是什么。
答案 0 :(得分:1)
您正在将validateOTPViewController
的新实例传递给displayalert
函数。
将其更改为:
displayalert1(title:"dsfvasdcs", message:"easfSDXCSDZX", vc:self)
这会将当前视图控制器传递给函数,而不是新的函数。
答案 1 :(得分:1)
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>)
}
}
答案 2 :(得分:0)
我运行你的代码,它工作正常。我想你会在vc中传递自我。
self.displayalert(title: "Title", message: "Some Message", vc: self)
您还可以扩展UIViewController -
extension UIViewController {
// Your Function...
}
现在您可以从任何视图控制器全局访问此功能,只需键入 -
即可 self.displayalert(title: "Title", message: "Some Message", vc: self)