向按钮添加条件(Swift)

时间:2016-09-20 00:02:23

标签: ios swift xcode firebase

我想为按钮添加条件。

因此,如果条件为真,则将通过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){}
    }

}

这是我尝试过的!但它没有给出我想要的结果。

1 个答案:

答案 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){}
    }
 }
}