faris97
faris97

Reputation: 402

Swift textfield automatic cursor

Is it possible to put a cursor automatically in the first of textfields, when opening a screen? For example, when I open a screen with Email and Password, I want to right after that type my email, without pressing a textfield.

Upvotes: 0

Views: 538

Answers (3)

javier rivarola
javier rivarola

Reputation: 510

If you have a static tableView, just take an IBOutlet from your textFields, and in the method viewWillAppear you can call to yourTextField.becomeFirstResponder()

If you have a dynamic tableView, you have some kind of data source object, you can loop in it, find the index of the first textField, then you have to scroll the tableView to that cell, so it becomes visible, and then you should get a reference to the cell with let cell = tableView.cellForRow(at: IndexPathOfYourCell) as? YourCustomTextCell, and then you can do something like cell.textField.becomeFirstResponder()

Upvotes: 2

MBT
MBT

Reputation: 1391

Create an IBOutlet of TextFiled in ViewController and in viewDidLoad() method call myTextField.becomeFirstResponder()

override func viewDidLoad() {
    super.viewDidLoad()

    myTextField.becomeFirstResponder()
}

Upvotes: 1

Mumtaz Hussain
Mumtaz Hussain

Reputation: 1125

Chose whichever textfield you want to become first responder:

override func viewDidLoad(){
  super.viewDidLoad()

  emailTextField.becomeFirstResponder()

}

Upvotes: 2

Related Questions