我想给按钮添加条件。
因此,如果条件为真,将通过 segue 打开另一个 View !
否则,什么也不会发生,并且会向用户显示警告。
这两个 View 由导航 Controller 链接。
@IBAction func AddButton(_ sender: AnyObject) {
if FIRAuth.auth()?.currentUser != nil {
self.performSegue(withIdentifier: "AddDevice", sender: nil)
}
else {
let alert = UIAlertController(title: "Sorry", message:"You have to Register First", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default) { _ in })
self.present(alert, animated: true){}
}
}
这是我试过的!但是并没有给出我想要的结果。
最佳答案
试试这个!
@IBAction func AddButton(_ sender: AnyObject) {
FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
if let user = user {
self.performSegue(withIdentifier: "AddDevice", sender: nil)
}
else {
let alert = UIAlertController(title: "Sorry", message:"You have to Register First", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default) { _ in })
self.present(alert, animated: true){}
}
}
}
关于ios - 向按钮添加条件(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39583771/