Install initialFirstResponder in an OSX storyboard application

Like this question , I am trying to set the first responder to the first of three NSTextField in a very simple application. The advice there (set Window first responder in the interface builder) seems to no longer apply, at least when storyboards are not involved. The Window object does have the initialFirstResponder output, but I cannot find a way to set it using drag, ctrl-drag, etc. In any direction to / from NSTextField.

The IB framework provided me with the Window Controller scheme (an empty window itself) associated with the View Controller scene (which, in turn, contains my set of NSTextField and shortcuts).

My suspicion is that there is some default premise / connection that Xcode baked in my application, which causes the Windows controller to automatically load a certain view at runtime, and therefore it is not possible to link the first responder that it does not know at about build time. Which then leaves me at a loss as to what life cycle is causing, which object is the right point to call self.view.window.initialFirstResponder = myTextField , until it is too late, and how to get access to this object most correctly. viewWillAppear or viewDidLoad in the View Controller is correct, but none of them have a reliable effect (the initially active NSTextField changes from start to start, sometimes appears to track code changes, but never happened).

I already posted the github code

+5
source share
1 answer

initialFirstResponder applies only when NSWindow first appears on the screen. According to the Xcode docs:

 `NSWindow initialFirstResponder` The view that's made first responder (also called the key view) the first time the window is placed onscreen. 

If you want to set something as the first responder, you need to call:

swift: yourTextField.becomeFirstresponder()

Obj-C: [yourTextField becomeFirstresponder];

Hope that helps!

Ade

change

I literally shaved your .m to a few lines:

  #import "ViewController.h" @implementation ViewController #pragma mark - View lifecycle -(void)viewWillAppear{ [super viewWillAppear]; //[self.userTextField becomeFirstResponder]; [self.pathTextField becomeFirstResponder]; } #pragma mark - Connection actions - (IBAction)connectClicked:(id)sender { } - (IBAction)cancelClicked:(id)sender { // Tell the app delegate (once we've actually got a pointer to it) that we want to exit // [self.delegate exitApplication]; } @end 

My fault! Sorry, viewWillAppear (just before the view actually appears), where you put startFirstResponder ..

I'm sorry for the mistake!

+2
source

Source: https://habr.com/ru/post/1241311/


All Articles