2017-01-02 23 views
1

Output Console如何在UITextfield數據爲空或填充時禁用/啓用按鈕?

這是我的功能。正如您在上面看到的,當文本字段爲空(或無)並且按鈕被禁用時,我可以在控制檯中收到消息。但是,當數據輸入到我的文本字段中時,我沒有收到回覆。非常感謝,如果有人可以幫助我解決這個或類似的解決方案。

func logicDataCheck() { 
    let textfield: Array<Bool?> = [lastNameTextField.text?.isEmpty, phoneNumberTextField.text?.isEmpty, firstNameTextField.text?.isEmpty] 

    if (textfield[0]! || textfield [1]! || textfield [2]!) 
    { 
     print("Textfield is empty") 
     continue_Button.isEnabled = false 
     continue_Button.alpha = 0.5 
    } 
    else 
    { 
     if (textfield[0]! || textfield [1]! || textfield [2]!) { 
      print("Textfield is full") 
      continue_Button.isEnabled = true 
      continue_Button.alpha = 1 
     } 
    } 
} 

回答

2

在你viewDidLoad,您可以添加以下行:

configureTextFields() 
// you'll need to call updateTextField() when the view loads the first time. 
// After the configureTextFields() adds targets, it'll get called when editing changes 
updateTextField() 

此外,添加這些方法:

func configureTextFields() { 
    // create an array of textfields 
    let textFieldArray = [firstNameTextField, lastNameTextField, phoneNumberTextField] 

    // configure them... 
    for textField in textFieldArray { 
     // make sure you set the delegate to be self 
     textField?.delegate = self 
     // add a target to them 
     textField?.addTarget(self, action: #selector(ViewController.updateTextField), for: .editingChanged) 
    } 
} 

// this is the target that gets called when editing changes 
func updateTextField() { 
    // create an array of textFields 
    let textFields = [firstNameTextField, lastNameTextField, phoneNumberTextField] 
    // create a bool to test if a textField is blank in the textFields array 
    let oneOfTheTextFieldsIsBlank = textFields.contains(where: {($0?.text ?? "").isEmpty}) 
    if oneOfTheTextFieldsIsBlank { 
     continueButton.isEnabled = false 
     continueButton.alpha = 0.5 
    } else { 
     continueButton.isEnabled = true 
     continueButton.alpha = 1.0 
    } 
} 

你需要讓你的ViewController採用UITextFieldDelegate協議。你可以這樣做,像這樣:

class ViewController: UIViewController, UITextFieldDelegate { 
// the code inside your view controller 
} 

這裏是一個鏈接到一個demo I created on GitHub

+0

這個工作!謝謝 –

+2

請注意'emptyCheck'實際上是一個可選字符串數組,當它們中的一個也是'nil'時,你應該禁用這個按鈕。因此檢查應該是'{$ 0!= false}'。此外,如果你定義了一個文本字段數組,例如代碼將更簡單。 'let textFields:[UITextField] = [firstNameTextField,...]'然後'let emptyCheck = textFields.contains(其中:{($ 0.text ??「」「).isEmpty})''。 – Sulthan

+0

@Sulthan太棒了。我根據你的意見進行了更新。 – Adrian

0

試試這個

func logicDataCheck() { 
    let textfield: Array<Bool> = [lastNameTextField.text?.isEmpty, phoneNumberTextField.text?.isEmpty, firstNameTextField.text?.isEmpty] 

    if (textfield[0] || textfield [1] || textfield [2]) 
    { 
     print("Textfield is empty") 
     continue_Button.isEnabled = false 
     continue_Button.alpha = 0.5 
    } 
    else 
    { 
     print("Textfield is full") 
     continue_Button.isEnabled = true 
     continue_Button.alpha = 1 
    } 
}