Set cursor focus on TextField when loading a form in Objective-C

I have 3 text fields (Username, Password, Confirm Password). When the form loads, I want the cursor to start in the Username text box when the form first displays. Please give me some advice. Thanks in advance.

+5
source share
7 answers

[usernameTextField becomeFirstResponder];

This should provide you with the desired result. You can read about the answer chain here if you want to know more about the mechanism.

Edit:

My bad, I thought you meant UITextField (Cocoa Touch).

Use the NSWindow makeFirstResponder: method, rather than this method, to make the object the first responder. Never call this method directly.

[window makeFirstResponder:usernameTextField];
+26

becomeFirstResponder, :

[self.passwordField becomeFirstResponder]
+2

[yourTextfield getFirstResponder]

+2

loadView viewDidLoad ( 10.10) super

dispatch_async(dispatch_get_current_queue(), ^{
    [self.usernameInputField becomeFirstResponder];
});
+1

Swift

self.sampleUITextField.becomeFirstResponder()

0

macOS, Swift 3, getFirstResponder() viewWillAppear().

class myViewController {
  @IBOutlet weak var myTextField: NSTextField!

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  override func viewWillAppear {
    myTextField.becomeFirstResponder()
  }
  ...
}
-1

All Articles