0

I have a problem when trying to delete a character from a secured textField text.

I want to capture the text in textFieldShouldChangeCharactersInRange delegate method, but the problem is that for secured textFields there is a problem. When I tap on the secured textField and delete on character from the textField's text, visually all the textField text disappears (this is by default for secured text fields).

The problem is that the text returned in textFiledShouldChangeCharactersInRange is not an empty text, but is the text without the removed character. So the problem is that visually there is no text left in the text field, but the text returned by the delegate method mentioned above is not nil, or empty.

I want to ask if is there any possibility to capture the correct text.

If something is not clear please ask and I will respond.

Thank you very much!

  • tell us why do you need that, because i don't see any real problem here, what do you want to use this text and when? – Lu_ Jul 28 '16 at 08:00
  • Show the code, please? – Feldur Jul 28 '16 at 08:11
  • I just want to get the correct textField text in real time and save it somewhere. And if the textField shows visually that there is no text letf, but the delegate methods returns the text without a character, instead of empty/nil text, there is an inconsistency for me betweeen visual and saved text. – Toparceanu Rares Jul 28 '16 at 08:33
  • Explain with some small example I am not getting the problem... I have tried with the secure textfield its working fine in my case... – Naveen Kumar H S Jul 28 '16 at 09:28

1 Answers1

0

try this

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    if let swRange = textField.text!.rangeFromNSRange(range)
    {
        let newString = textField.text!.stringByReplacingCharactersInRange(swRange,withString:string) //...
        print(newString)
    }
    return true
}

the method rangeFromNSRange(range) can be founded here NSRange to Range<String.Index>

I hope this helps you

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55