Set cursor focus on TextField when form load in Objective C

| | August 4, 2015

I have 3 textfields (Username, Password, Confirm Password). When form load I want to set cursor on textfield Username. Please give me some advice. Thank in advance.

6 Responses to “Set cursor focus on TextField when form load in Objective C”

  1. In Swift

    self.sampleUITextField.becomeFirstResponder()

  2. If you’re trying to achieve this in a NSPanel (or any other sheet) and you disable the title+border, the focus cannot be set (see http://www.cocoabuilder.com/archive/cocoa/207749-nstextfield-will-not-become-editable-in-borderless-window.html)

    In that case, try to subclass the sheet window class and override canBecomeKeyWindow to always return YES.

  3. Vladimir Prudnikov on November 30, -0001 @ 12:00 AM

    In your loadView or viewDidLoad (which is available in 10.10 only) add the following after super call

    dispatch_async(dispatch_get_current_queue(), ^{
        [self.usernameInputField becomeFirstResponder];
    });
    
  4. [yourTextfield becomeFirstResponder]

  5. [usernameTextField becomeFirstResponder];

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

    Edit:

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

    Use the NSWindow makeFirstResponder: method, not this method, to make
    an object the first responder. Never invoke this method directly.

    [window makeFirstResponder:usernameTextField];
    
  6. Use the message becomeFirstResponder, like so:

    [self.passwordField becomeFirstResponder]
    

Leave a Reply