私はココアを学ぶためだけに小さなアプリに取り組んでおり、FirstResponderをいくつかのNSTextFieldsに設定するのに苦労しています。
ビューが開いたら、最初のNSTextFieldであるclientNumberを選択する必要があるため、loadViewメソッドの最後で[clientNumber beenFirstResponder]をトリガーしますが、テキストフィールドは選択されません。しかし、(IBAction)addToArrayメソッドを起動するボタンをクリックすると、適切なテキストフィールドが選択されます。さらに、別のテキストフィールドがありますか。整数のみを含める必要があるため、大まかな検証を行います。コンテンツがintでない場合、本来のようにビープ音が鳴りますが、テキストフィールドが選択されません。
これが私のコードです:
- (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];
}
}
誰かがここで私を助けてくれることを本当に望んでいます。
編集:
ポインタのおかげですべてが間違っていましたが、コードを修正するときは、次を使用します。FakturaAppDelegate * appDelegate =(FakturaAppDelegate *)[[NSApplicationsharedApplication]デリゲート]; [appDelegate.window makeFirstResponder:self.invoiceCredit]; 代わりにbecomeFirstResponder。しかし、それでも機能しません。