1

so i search online about how "func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool" for formating textField. i found this way to prevent "0" as my first char in my textfield. it works but, actually not the way i wanted.

The thing is, in my register view theres 4 uiTextfield, which contain :

  • Name
  • Phone Number
  • Pin
  • Confirm Pin

i only want to prevent "0" as my first char only for my PhoneNum TextField. but with this code, i kinda put all of those 4 textField into prevent "0" as the first char.

here's my code :

@IBOutlet weak var textFullName: SkyFloatingLabelTextField!
@IBOutlet weak var textPhoneNumber: SkyFloatingLabelTextField!
@IBOutlet weak var textPIN: SkyFloatingLabelTextField!
@IBOutlet weak var textConfirmPIN: SkyFloatingLabelTextField!


func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    // I try this one
    if string == "0" {
        if textField.text!.count == 0 {
            return false
        }
    }
    
    // i also try to change the "textField" into "textPhoneNumber"
    if string == "0" {
        if textPhoneNumber.text!.count == 0 {
            return false
        }
    }
    

   // both of those method wont work
  
    return true
}

can someone tell me how to just prevent my textPhoneNumber to cannot type "0" as its first char? with this method i only put all of my textfield into cannot type "0" as its first char.

Thanks guys

afi permana
  • 207
  • 3
  • 17
  • `if textField == textPhoneNumber { do the stuff check } else { return true }` Can be written with a `guard else` too: `guard textField == textPhoneNumber else { return true } do the stuff check` – Larme Jul 29 '20 at 14:31
  • Also, before doing your test, you should discover what the resulting string will be. See answer to https://stackoverflow.com/questions/25621496/how-shouldchangecharactersinrange-works-in-swift . That approach handles the case where the user types "90", then goes back and deletes the "9". – Phillip Mills Jul 29 '20 at 14:39

2 Answers2

3

The UITextFieldDelegate methods will get called for ALL of the text fields for which the view controller is set up as the delegate.

Presumably your textField(_:shouldChangeCharactersIn:replacementString:) method is being called for all 4 of your SkyFloatingLabelTextField objects (which I assume are a custom subclass of UITextField.)

If you want different behavior for different text fields, you need to write your code to handle each case separately. Something like this:

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

    switch textField {
        case textPhoneNumber:
            //Special case code to handle the phone number field
            if string == "0" {
                if textField.text!.count == 0 {
                   return false
                }
                return true
            }

        case textFullName:
            //Special case code to handle the name field (if needed.)
        default:
            //Shared code to handle the other fields the same way
    }
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

You can check if the textField is textPhoneNumber directly by using the equatability == sign.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField == textPhoneNumber {
        // do textPhoneNumber stuff
    } else {
        // others
    }
    return true
}
Frankenstein
  • 15,732
  • 4
  • 22
  • 47