2014-09-23 56 views
2

對於UITextFieldDelegate,我將根據3 UITextFields是否包含信息來禁用我的UIButton「btnSignup」。目前,它適用於減去UIButton被動(再次運行if聲明)的事實,我必須點擊UITextField。當我輸入某些內容並且回去刪除它時也是如此。直到我點擊UITextField的關UIButton保持有效(如果我點擊提交沒有點擊其他地方,我能夠提交,這不應該是這樣。)禁用帶有textFieldDidBeginEditing的UIButton需要單擊UITextField才能生效

class MainVC: UIViewController, UITextFieldDelegate { 

    @IBOutlet var receiveName: UITextField! 
    @IBOutlet var receiveEmail: UITextField! 
    @IBOutlet var receivePhone: UITextField! 
    @IBOutlet var btnSignup: UIButton! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.btnSignup.enabled = false 
     self.btnSignup.alpha = 0.3 

     self.receiveName.delegate = self 
     self.receiveEmail.delegate = self 
     self.receivePhone.delegate = self 

    func textFieldDidBeginEditing(textField: UITextField) { 

     if countElements(receiveName.text) > 0 && countElements(receiveEmail.text) > 0 && countElements(receivePhone.text) > 0 { 
      self.btnSignup.enabled = true 
      self.btnSignup.alpha = 1 
     } else { 
      self.btnSignup.enabled = false 
      self.btnSignup.alpha = 0.3 
     } 
    } 
} 

我要尋找一個解決方案,讓更實時的效果。如果在編輯UITextField的任何時候,該函數應該在監聽並響應。

回答

1

用接近你的代碼的東西,我可以如下做到這一點。我增加了3個職業等級。要記錄文本字段是否有輸入內容的變量。我給文本字段添加了標籤。我更改了UITextFieldDelegate類的func,並測試了所有三個字段都有文本。第二個你在第三個字段中輸入一個字段,該按鈕點亮!

import UIKit 

class ViewController: UIViewController, UITextFieldDelegate { 
    var receiveNameBool = false 
    var receiveEmailBool = false 
    var receivePhoneBool = false 

    @IBOutlet var receiveName: UITextField! 
    @IBOutlet var receiveEmail: UITextField! 
    @IBOutlet var receivePhone: UITextField! 
    @IBOutlet var btnSignup: UIButton! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.btnSignup.enabled = false 
     self.btnSignup.alpha = 0.3 

     self.receiveName.delegate = self 
     self.receiveEmail.delegate = self 
     self.receivePhone.delegate = self 

     } 

    func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool { 

     if textField.tag == 1 { 
      receiveNameBool = true 
     } else if textField.tag == 2 { 
      receiveEmailBool = true 
     } else if textField.tag == 3 { 
      receivePhoneBool = true 
     } 

     if receiveNameBool && receiveEmailBool && receivePhoneBool { 
      self.btnSignup.enabled = true 
      self.btnSignup.alpha = 1 
     } else { 
      self.btnSignup.enabled = false 
      self.btnSignup.alpha = 0.3 
     } 
     return true 
    } 
} 

好的,在這第二個版本中,我使用了你使用的textFieldDidBeginEditing func。在這種情況下,您點擊第三個字段內的第二個按鈕已啓用。在這兩個示例中,如果用戶刪除他們輸入的數據,您仍需編寫代碼來處理這種情況。但上面的第一個例子讓你得到你想要的。

進口的UIKit

class ViewController: UIViewController, UITextFieldDelegate { 
    var receiveNameBool = false 
    var receiveEmailBool = false 
    var receivePhoneBool = false 

    @IBOutlet var receiveName: UITextField! 
    @IBOutlet var receiveEmail: UITextField! 
    @IBOutlet var receivePhone: UITextField! 
    @IBOutlet var btnSignup: UIButton! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.btnSignup.enabled = false 
     self.btnSignup.alpha = 0.3 

     self.receiveName.delegate = self 
     self.receiveEmail.delegate = self 
     self.receivePhone.delegate = self 

     } 

    func textFieldDidBeginEditing(textField: UITextField) { 

     if textField.tag == 1 { 
      receiveNameBool = true 
     } else if textField.tag == 2 { 
      receiveEmailBool = true 
     } else if textField.tag == 3 { 
      receivePhoneBool = true 
     } 

     if receiveNameBool && receiveEmailBool && receivePhoneBool { 
      self.btnSignup.enabled = true 
      self.btnSignup.alpha = 1 
     } else { 
      self.btnSignup.enabled = false 
      self.btnSignup.alpha = 0.3 
     } 
    } 
} 
+0

我複製並粘貼了你在這裏完成的工作,但實際上它根本沒有激活btnSignup。它從未重新激活。在回答時你能留下什麼嗎? – davidrayowens 2014-09-23 03:00:17

+0

您是否連接了IBOutlets?您是否將標籤添加到屬性檢查器中的文本字段? – 2014-09-23 03:02:28

+0

是的,他們已經從頭開始連接 – davidrayowens 2014-09-23 03:07:47

0

爲此,您可以使用文本字段委託方法stringByReplacingCharactersInRange

enum tags: Int{ 

case receiveNameTag = 1, receiveEmailTag, receivePhoneTag 


} 

進口的UIKit

類的ViewController:UIViewController中,UITextFieldDelegate {

@IBOutlet var receiveName: UITextField! 
@IBOutlet var receiveEmail: UITextField! 
@IBOutlet var receivePhone: UITextField! 
@IBOutlet var btnSignup: UIButton! 

// here first assign the true because all the textfields are empty 
var isReceiveNameTextFieldEmpty = true 
var isReceiveEmailTextFieldEmpty = true 
var isReceivePhoneTextFieldEmpty = true 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    self.btnSignup.enabled = false 
    self.btnSignup.alpha = 0.3 


    //here adding tags to the individual textfield and this is the best way to use tags 
    receiveName.tag = tags.receiveNameTag.toRaw() 
    receiveEmail.tag = tags.receiveEmailTag.toRaw() 
    receivePhone.tag = tags.receivePhoneTag.toRaw() 

    self.receiveName.delegate = self 
    self.receiveEmail.delegate = self 
    self.receivePhone.delegate = self 

} 

func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool { 

    textField.text = (textField.text as NSString).stringByReplacingCharactersInRange(range, 
     withString:string) 

    //checking whether textfield is empty or not 
    if countElements(textField.text) > 0 { 

     if textField.tag == tags.receiveNameTag.toRaw() { 
      isReceiveNameTextFieldEmpty = false 
     } 
     if textField.tag == tags.receiveEmailTag.toRaw() { 
      isReceiveEmailTextFieldEmpty = false 
     } 
     if textField.tag == tags.receivePhoneTag.toRaw() { 
      isReceivePhoneTextFieldEmpty = false 
     } 

    } 
    else { 

     if textField.tag == tags.receiveNameTag.toRaw() { 
     isReceiveNameTextFieldEmpty = true 
     } 
     if textField.tag == tags.receiveEmailTag.toRaw() { 
      isReceiveEmailTextFieldEmpty = true 
     } 
     if textField.tag == tags.receivePhoneTag.toRaw() { 
      isReceivePhoneTextFieldEmpty = true 
     } 

    } 

    //if atleast one textfield is empty disable button other wise enable 
    if isReceiveNameTextFieldEmpty && isReceiveEmailTextFieldEmpty && isReceivePhoneTextFieldEmpty { 
     self.btnSignup.enabled = false 
     self.btnSignup.alpha = 0.3 
    } 
    else { 
     self.btnSignup.enabled = true 
     self.btnSignup.alpha = 1 
    } 


} 

}

+0

不幸的是,當我在我的textFieldDidBeginEditing函數中添加相同的代碼時,這並不起作用。 – davidrayowens 2014-09-23 02:18:53

+0

在textFieldDidBeginEditing中註釋您的代碼並執行您的代碼。 – 2014-09-23 03:16:07

+0

請在範圍方法中改變字符處的中斷點,並檢查這些文本字段是否爲空。只需在每個文本字段中鍵入兩個字符並刪除一個文本字段文本。希望你能找到答案。 – 2014-09-23 03:30:43

0

我也有類似的問題,我的應用程序之一。它發生在我身上,因爲委託在後臺線程上調用,不會立即更新視圖。您需要將所有UI更改同步到主線程。請參見下面的代碼:

class MainVC: UIViewController, UITextFieldDelegate { 

    @IBOutlet var receiveName: UITextField! 
    @IBOutlet var receiveEmail: UITextField! 
    @IBOutlet var receivePhone: UITextField! 
    @IBOutlet var btnSignup: UIButton! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.btnSignup.enabled = false 
     self.btnSignup.alpha = 0.3 

     self.receiveName.delegate = self 
     self.receiveEmail.delegate = self 
     self.receivePhone.delegate = self 

    func textFieldDidBeginEditing(textField: UITextField) { 
     dispatch_sync(dispatch_get_main_queue(), { 
      if countElements(self.receiveName.text) > 0 && countElements(self.receiveEmail.text) > 0 && countElements(self.receivePhone.text) > 0 { 
       self.btnSignup.enabled = true 
       self.btnSignup.alpha = 1 
      } else { 
       self.btnSignup.enabled = false 
       self.btnSignup.alpha = 0.3 
      } 
     }) 
    } 
} 

另外,如果您countElements功能是本地的類,在電話前加上一個self.

0

它可以通過textField(_:shouldChangeCharactersInRange:replacementString:) 只是定義與界定這種方法如下解決:

func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool { 

     if countElements(receiveName.text) > 0 && countElements(receiveEmail.text) > 0 && countElements(receivePhone.text) > 0 { 
      self.btnSignup.enabled = true 
      self.btnSignup.alpha = 1 
     } else { 
      self.btnSignup.enabled = false 
      self.btnSignup.alpha = 0.3 
     } 
     return true 
    } 

,我認爲countElements()被計算字符串的長度。

相關問題