BriScoLeg
BriScoLeg

Reputation: 105

New view controller vs. hiding textfields with .isHidden = true

Disclaimer: I'm new to coding.

I'm making a personal finance app. When the user creates a new transaction, I want them to be presented with a single textfield at a time.

In a single view controller, I've created a next button with a switch statement to hide/unhide the text fields:

@IBAction func nextPressed(_ sender: UIButton) {
    
    buttonCounter += 1
        
    switch buttonCounter {
    case 1:
        currencyTextField.isHidden = true
        nameTextField.isHidden = false
            
    case 2:
        nameTextField.isHidden = true
        setupDateView()
            
    case 3:
        saveTransaction()
            
    default:
        print("Error")
    }
}

It works really well, but I'm running into some issues with the UITextfield Delegate with multiple text fields.

I can get around this with a @IBDesignable class customTextField: UITextField, but before I continue I was wondering if this is bad programming practice. Am I overcomplicating it by only using a single view controller? What is the best practice for this situation?

Thanks in advance.

Upvotes: 0

Views: 59

Answers (1)

Asif Mujtaba
Asif Mujtaba

Reputation: 477

Is it the issue that you can't track multiple text fields text in the delegate method?

If that is the case then the easiest way is to know what text field to use in delegate methods. As you have two text fields currencyTextField and nameTextField.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField == currencyTextField {
        // do something
    } else if textField == nameTextField {
        // do something
    }
    return true
}

Let me know if it is helpful.

Upvotes: 1

Related Questions