I need the text of the text field to be selected right after the UIAlertController is presented. However, the way I select text in a standard UITextField doesn't work here.
This is what I tried, but I can't seem to get it work.
let ac = UIAlertController(title: "Rename", message: nil, preferredStyle: .Alert)
ac.addTextFieldWithConfigurationHandler({
[] (textField: UITextField) in
textField.selectedTextRange = textField.textRangeFromPosition(textField.beginningOfDocument, toPosition: textField.endOfDocument)
textField.text = "filename.dat"
})
ac.addAction(UIAlertAction(title: "CANCEL", style: .Cancel, handler: nil))
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: {
[] Void in
// do something
}))
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(ac, animated: true, completion: nil)
})
Any ideas?
textField.becomeFirstResponder()
from @ridvankucuk's answer. Are there situations where that line should be added back in? – Cagliari