我想在表单加载时设置焦点,但它在 awakeFromNib 中不起作用。
[myTextField becomeFirstResponder];
只是引用苹果文档:
Use the NSWindow makeFirstResponder: method, not this method, to make an object the first responder. Never invoke this method directly.
改为这样做:
[[myTextField window] makeFirstResponder:myTextField];
这个问题的 Swift 版本:
textField.window?.makeFirstResponder(textField)
- (BOOL) becomeFirstResponder
{
if (someCondition)
{
return NO;
}
BOOL retVal = [super becomeFirstResponder];
// do your stuff
return retVal;
}