gpt4 book ai didi

objective-c - 在 Cocoa Mac OSX 中设置 FirstResponder 时出现问题

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:47 25 4
gpt4 key购买 nike

我正在开发一个小应用程序只是为了学习 cocoa ,我很难将 FirstResponder 设置为某些 NSTextFields。

当 View 打开时,我想要选择第一个 NSTextField clientNumber,因此我在 loadView 方法的末尾触发了 [clientNumber becomeFirstResponder],但它没有选择文本字段。但是当我单击触发 (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 *)[[NSApplication sharedApplication] delegate]; [appDelegate.window makeFirstResponder:self.invoiceCredit];而不是成为第一响应者。但它仍然不起作用。

最佳答案

你让这件事变得比它需要的要难得多。

在 Interface Builder 中,将窗口的 initialFirstResponder 导出设置为指向文本字段。

就是这样。

如果您绝对必须以编程方式执行此操作,请使用:

[window setInitialFirstResponder:yourTextField];

请记住,如果您正在与 Cocoa 争吵来做一些看起来应该很简单的事情,那么您可能做错了。

关于objective-c - 在 Cocoa Mac OSX 中设置 FirstResponder 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7263482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com