2016-04-25 26 views
0

我試圖阻止用戶操縱他們的數據,所以當他們點擊UITextfield時,alertController只會按「yes」選項後纔會顯示一次,那麼他們應該可以更改數據。但不是一次顯示警報控制器,而是每次點擊UITextfield時都會一直顯示;我如何解決這個問題。如何僅在點擊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() 
} 

回答

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() 
} 
+0

你簡單的技巧幫助我,不準確,因爲我想,但它解決了我的問題。 謝謝 – User

相關問題