Configure FirstResponder Settings on Cocoa Mac OSX

I am working on a tiny application to learn cocoa, and it is very difficult for me to set the FirstResponder parameter to some NSTextFields.

When the view opens, I want the first NSTextField, clientNumber to be selected, so I run [clientNumber getFirstResponder] at the end of my loadView method, but it does not select the text field. But when I click on the button that launches the addToArray (IBAction) method, it selects the correct text field. In addition, I have another text field, it should contain only integers, so I have a rough check. When the content is not int, I get a beep as it should, but it does not select the text field.

Here is my code:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { NSLog(@"initWithNibName"); } return self; } - (void)viewWillLoad { NSLog(@"viewWillLoad"); } - (void)viewDidLoad { NSLog(@"viewDidLoad"); [self initNewInvoice]; } - (void)loadView { NSLog(@"loadView"); [self viewWillLoad]; [super loadView]; [self viewDidLoad]; FakturaAppDelegate *appDelegate = (FakturaAppDelegate *)[[NSApplication sharedApplication] delegate]; [appDelegate.window makeFirstResponder:self.clientNumber]; } - (void)awakeFromNib { NSLog(@"awakeFromNib"); } - (IBAction)saveInvoice:(id)sender { FakturaAppDelegate *appDelegate = (FakturaAppDelegate *)[[NSApplication sharedApplication] delegate]; [appDelegate.window makeFirstResponder:self.invoiceCredit]; } - (IBAction)addToArray:(id)sender { [clientNumber setStringValue: @"Toodeloo"]; FakturaAppDelegate *appDelegate = (FakturaAppDelegate *)[[NSApplication sharedApplication] delegate]; [appDelegate.window makeFirstResponder:self.clientNumber]; } - (IBAction)updateCreditDays:(id)sender { if(invoiceCredit.intValue){ [self updateCredit]; }else{ NSBeep(); FakturaAppDelegate *appDelegate = (FakturaAppDelegate *)[[NSApplication sharedApplication] delegate]; [appDelegate.window makeFirstResponder:self.invoiceCredit]; } } 

I really hope someone can help me here.

EDIT:

Everything turned out wrong for me, thanks for the pointers, but when I fix the code using this: FakturaAppDelegate * appDelegate = (FakturaAppDelegate *) [[NSApplication sharedApplication] delegate]; [appDelegate.window makeFirstResponder: self.invoiceCredit]; instead of startFirstResponder. But it still does not work.

+4
source share
5 answers

You make it a lot harder than it should be.

In the Builder interface, set the initialFirstResponder output of your window to point to the text field.

What is it.

If you absolutely must do this programmatically, use:

 [window setInitialFirstResponder:yourTextField]; 

Remember that if you are fighting Cocoa to make something similar, it should be simple, then you are probably wrong.

+2
source

Override viewDidAppear and inside the specified method call the NSTextField getFirstResponder method.

Note that in my own work, viewWillAppear was too early for the specified call using the NSTextField getFirstResponder method or the NSWindow makeFirstResponder method.

+1
source

The problem is that self.view.window is null in the first view of the viewDidLoad controller until the applicationDidFinishLaunching application terminates. Thus, startFirstResponder does not work in viewDidLoad. Try to delay setting up the first responder, for example:

[_ yourTextField makes a choice: @selector (startFirstResponder) withObject: nil afterDelay: 0.1];

0
source

You should never try to set the first responder in the viewDidLoad method, because the window is still zero at this point. Use viewWillAppear instead:

 - (void)viewWillAppear { [super viewWillAppear]; if (!_started) { _started = YES; [self.view.window makeFirstResponder:_yourView]; } } 

Apple also once stated that you should never call beFirstResponder directly. Use [yourWindow makeFirstResponder: yourView],.

0
source

Just read the documentation:

becomeFirstResponder

Tells the recipient that he is about to become the first responder at NSWindow.

[...]

Use the NSWindow makeFirstResponder: method, rather than this method, to make the object the first responder. Never call this method directly.

-2
source

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


All Articles