NSTextField becomeFirstResponder not work in awakeFromNib

前端 未结 3 1788
夕颜
夕颜 2021-01-05 02:41

I want to set focus when form load but it didn\'t work in awakeFromNib.

[myTextField becomeFirstResponder];
相关标签:
3条回答
  • 2021-01-05 02:56
    - (BOOL) becomeFirstResponder
     {
     if (someCondition)
     {
         return NO;
     }
    
     BOOL retVal = [super becomeFirstResponder];
    
     // do your stuff
     return retVal;
    }
    
    0 讨论(0)
  • The Swift version to this question:

    textField.window?.makeFirstResponder(textField)
    
    0 讨论(0)
  • 2021-01-05 03:12

    Just to quote the apple docs:

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

    Do this instead:

    [[myTextField window] makeFirstResponder:myTextField];
    
    0 讨论(0)
提交回复
热议问题