点击UITextfield时如何只显示一次警报控制器?

时间:2016-04-25 22:19:26

标签: swift uitextfield uialertcontroller

我试图阻止用户操纵他们的数据,所以当他们点击UITextfield时,alertController只会显示一次,然后他们按下#34;是&#34 ;选项,然后他们应该能够改变数据。但是我没有显示一次警报控制器,而是每次点击UITextfield时都会显示;我怎么能解决这个问题。

 func textFieldDidBeginEditing(textField: UITextField) {
       let alert = UIAlertController(title: "Warning !", message: "Are your sure you want to change your content? ", preferredStyle: .Alert)

           let yes = UIAlertAction(title: "Yes", style: .Cancel) { (action) in

              }
               alert.addAction(yes)
              let no = UIAlertAction(title: "No", style: .Default) { (action) in
        }
               alert.addAction(no)
             presentViewController(alert, animated: true) {}
        }
 textField.becomeFirstResponder()
}

1 个答案:

答案 0 :(得分:0)

var alertWillShow = true

 func textFieldDidBeginEditing(textField: UITextField) {

if alertWillShow {
       alertWillShow = false

       let alert = UIAlertController(title: "Warning !", message: "Are your sure you want to change your content? ", preferredStyle: .Alert)

           let yes = UIAlertAction(title: "Yes", style: .Cancel) { (action) in

              }
               alert.addAction(yes)
              let no = UIAlertAction(title: "No", style: .Default) { (action) in
        }
               alert.addAction(no)
             presentViewController(alert, animated: true) {}
        }
}
 textField.becomeFirstResponder()
}