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.
source share