Dirk Hildebrand Dirk Hildebrand - 3 months ago 12
Swift Question

Swift Select all text of a UITextfield from UITableviewCell Class

I want to select ALL text in a

UITextField
i.e. inside a
UITableviewCell
. But every Solution i find here wont work. It never selects the text of the
UITextfield
I tapped into.

Here is me Code (simplified). Does anyone know where the problem could be?

class generalTextFieldTableViewCell: UITableViewCell {
@IBOutlet weak var textField: UITextField!
@IBAction func textFieldDidBeginEditing(sender: UITextField) {
sender.becomeFirstResponder()
sender.delegate = self.textField.delegate
sender.selectAll(self)
}
}

Answer

In cellForRowAtIndexPath set the delegate of textField with current ViewController and try to use textFieldDidBeginEditing delegate method of UITextField.

func textFieldDidBeginEditing(textField: UITextField) {
    textField.selectAll(nil)
}

Note: Don't forgot to set the textField delegate inside cellForRowAtIndexPath like this cell.textField.delegate = self

Comments