Select text in UIAlertController's text field
Asked Answered
W

3

12

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?

Wolffish answered 14/3, 2016 at 15:35 Comment(0)
S
15

I have rewrote your code. Your class should conform to the UITextFieldDelegate protocol and implement the textFieldDidBeginEditing method, like this:

class ViewController: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let ac = UIAlertController(title: "Rename", message: nil, preferredStyle: .Alert)
        ac.addTextFieldWithConfigurationHandler({
            [] (textField: UITextField) in
            textField.text = "filename.dat"
            textField.delegate = self

        })
        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)
        })

    }
    func textFieldDidBeginEditing(textField: UITextField) {
        textField.selectedTextRange = textField.textRangeFromPosition(textField.beginningOfDocument, toPosition: textField.endOfDocument)
        textField.becomeFirstResponder()
    }

}
Strained answered 14/3, 2016 at 15:50 Comment(0)
S
10

A way to select all text without adding a delegate:

present(vc, animated: true) {
    vc.textFields?.first?.selectAll(nil)
}
Sassenach answered 21/12, 2017 at 16:58 Comment(0)
L
8

Thanks, @ridvankucuk. Your solution works great.

But textfield delegate function can be simplified little bit:

func textFieldDidBeginEditing(_ textField: UITextField) {
    textField.selectAll(nil)
}
Lindberg answered 31/10, 2016 at 11:6 Comment(2)
I notice you omitted textField.becomeFirstResponder() from @ridvankucuk's answer. Are there situations where that line should be added back in?Cagliari
@ToolmakerSteve, personally, I have never met with such situations. Text field is already first responder when textFieldDidBeginEditing: method is called. But may be you have some special case, when you need to make it first responder again.Lindberg

© 2022 - 2024 — McMap. All rights reserved.