在Cocoa Mac OSX中设置FirstResponder的问题 [英] Problems setting FirstResponder in Cocoa Mac OSX

查看:1680
本文介绍了在Cocoa Mac OSX中设置FirstResponder的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个小应用程序上工作,只是为了学习可可,我很难在一些NSTextFields设置FirstResponder。



当视图打开时,我想要第一个NSTextField,clientNumber,要选择,所以我在我的loadView方法的末尾触发[clientNumber becomeFirstResponder],但它没有选择文本字段。
但是当我点击激活(IBAction)addToArray方法的按钮时,它会选择正确的文本字段。此外,我有另一个文本字段,它应该只包含整数,所以我有一个粗略的验证。当内容不是一个int,我得到一个哔声,就像它应该,但它不选择文本字段。



这里是我的代码:

   - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
self = [super initWithNibName:nibNameOrNil bundles: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];
}
}

我真的希望有人能在这里帮助我。 / p>

编辑:



我得到了所有错误,感谢指针,但是当我修复代码,这个:
FakturaAppDelegate * appDelegate =(FakturaAppDelegate *)[[NSApplication sharedApplication] delegate];
[appDelegate.window makeFirstResponder:self.invoiceCredit];
而不是becomeFirstResponder。

解决方案

你要做的比这要困难得多。



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



这是它。



如果你绝对必须以编程方式,使用:

  [window setInitialFirstResponder:yourTextField]; 

请记住,如果你与Cocoa做一些似乎应该简单的事情,你可能做错了。


Im working on a tiny app just to learn cocoa, and Im having a hard time with setting FirstResponder to some NSTextFields.

When the view opens, I want the first NSTextField, clientNumber, to be selected, so I trigger [clientNumber becomeFirstResponder] at the end of my loadView method, but it does not select the text field. But when I click the button that fires the (IBAction)addToArray method, it selects the right text field. Further do I have another text field, it should contain integers only, so I have a crude verification for it. When the content is not a int, I get a beep, just like 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 out here.

EDIT:

I got it all wrong, 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 becomeFirstResponder. But it still doesnt work.

解决方案

You are making this much harder than it needs to be.

In Interface Builder, set the initialFirstResponder outlet of your window to point to your text field.

That's it.

If you absolutely must do it programmatically, use:

[window setInitialFirstResponder:yourTextField];

Remember, if you're fighting with Cocoa to do something that seems like it should be simple, then you're probably doing it wrong.

这篇关于在Cocoa Mac OSX中设置FirstResponder的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆