Reputation: 63
I have multiple restrictions I would like to add to 2 seperate textFields.
textField1 , textField2
Both Text Fields
Only Allow One Decimal
Only allow 8 characters in length
"shouldChangeCharactersIn" - How can I use this function to apply to multiple textFields and have multiple constraints?
func textField(_ textField: UITextField,shouldChangeCharactersIn range: NSRange,replacementString string: String) -> Bool
{
let countdots = (capitalInvested.text?.components(separatedBy: ".").count)! - 1
if countdots > 0 && string == "."
{
return false
}
return true
}
Upvotes: 0
Views: 2324
Reputation: 28902
You need to check which text field is being typed in, which calls the function:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == textField1 {
// first text field
// add restrictions here
} else if textField == textField2 {
// second text field
// add restrictions here
}
if textField == textField1 || textField == textField2 {
// restrictions for both
}
}
Upvotes: 4