我想为按钮添加条件。
因此,如果条件为真,则将通过segue打开另一个视图!
否则,没有任何反应,并且会向用户显示警告。
两个视图由导航控制器链接。
@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){}
}
}
这是我尝试过的!但它没有给出我想要的结果。
答案 0 :(得分:2)
试试这个!
@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){}
}
}
}