I found out why myself.
I create a custom NSWindowController:
@implementation MainWC
- (void)windowDidLoad {
[super windowDidLoad];
MainVC* vc = (MainVC*)self.contentViewController;
[vc windowLoaded];
}
@end
And in MainVC:
- (void)windowLoaded {
[self.view.window addObserver:self
forKeyPath:@"firstResponder"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context
{
NSView* oldView = change[NSKeyValueChangeOldKey];
NSView* newView = change[NSKeyValueChangeNewKey];
NSLog(@"%@ (%@) ====> %@ (%@)", oldView.identifier, oldView.class, newView.identifier, newView.class);
}
It seems when a textfield is focused, it immediately resigns and gives focus to its inner textview:
2023-03-24 18:02:54.841247+0800 ObjcTest[25855:1521206] _NS:10 (NSWindow) ====> normal (NSTextField)
2023-03-24 18:02:54.846902+0800 ObjcTest[25855:1521206] normal (NSTextField) ====> (null) (NSTextView)
2023-03-24 18:02:56.032290+0800 ObjcTest[25855:1521662] Sleep done in q
2023-03-24 18:03:09.653850+0800 ObjcTest[25855:1521206] (null) (NSTextView) ====> _NS:10 (NSWindow)
2023-03-24 18:03:09.657517+0800 ObjcTest[25855:1521206] _NS:10 (NSWindow) ====> mytextfield (MyTextField)
2023-03-24 18:03:09.657677+0800 ObjcTest[25855:1521206] -[MyTextField becomeFirstResponder]: <MyTextField: 0x7f8dfa123eb0>
2023-03-24 18:03:09.659349+0800 ObjcTest[25855:1521206] -[MyTextField resignFirstResponder]: <MyTextField: 0x7f8dfa123eb0>
2023-03-24 18:03:09.659522+0800 ObjcTest[25855:1521206] mytextfield (MyTextField) ====> (null) (NSTextView)