在 NSSearchField 上按下功能键而无需子类化
我有一个简单的应用程序,其中有一个 NSSearchField
,我想知道用户是否按下了例如箭头键。我不想子类化,因为我想修改应用程序中 IBOutlet 的值,但无法在子类中执行此操作。
编辑
我在 NSSearchField
子类中重写的 keyUp:
方法是:
-(void)keyUp:(NSEvent*)event
{
if ([event keyCode]==36){
customers* c= [[customers alloc] init];//customers is the class where i have my IBOulets and my methods
[[self window] selectKeyViewFollowingView:self];// change the first responder
[c searcht:[self stringValue]];//here i want to call a method to make a query and change the stringValues of some IBOulets
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将您的控制器指定为搜索字段的委托并实现该方法:
然后您应该能够接收简单的 NSResponder 选择器,例如与箭头键相对应的 moveDown:、moveUp:、moveLeft:、moveRight:。
Assign your controller as the delegate of your searchfield and implement the method:
You should then be able to receive simple NSResponder selectors such as moveDown:, moveUp:, moveLeft:, moveRight: which correspond to the arrow keys.
NSSearchField 继承自 NSControl,它具有您正在寻找的委托方法。
将 NSSearchField 设置为您的委托并实现此方法。可能需要运行检查以确保是您的 NSSearchField 开始编辑。也许您可以使用 FirstResponder。类似于:
http://developer.apple.com/library/mac/#documentation/cocoa/reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html#//apple_ref/occ/cl/NSControl
NSSearchField inherits from NSControl which has the delegate method you are looking for.
Set the NSSearchField as your delegate and implement this method. Might want to run a check to make sure it's your NSSearchField that's started editing. Maybe you could use FirstResponder. Something like:
http://developer.apple.com/library/mac/#documentation/cocoa/reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html#//apple_ref/occ/cl/NSControl
无法获取已传递给您没有代码的视图的
NSEvent
。如果要更改视图或控件的键处理行为,则必须进行子类化。您也许可以在响应者链中的搜索字段之前插入另一个对象,获取事件,执行您想要的任何操作,然后就像您没有处理它一样,并将其传递到字段……只是我脑海中的一个疯狂的想法。更新回复您的
keyUp:
代码:好的,问题正在变得清晰。当您在代码中创建新的
customer
* 对象时,您还必须在代码中连接它的连接。他们没有为你连接。您实际上并没有为此使用IBOutlets
,而只是使用常规的旧 ivars。IBOutlets
的存在只是为了让您可以在 Interface Builder 的图形界面中连接对象。我不知道你到底想在searcht:
中做什么,但举个例子,如果你想更改文本字段的内容:其中 customer.h 看起来像:
这不会执行任何操作,包括不发出警告,因为
myTextField
为nil
。您可以整天调用nil
上的方法,但什么也不会发生。您需要做的是为customer
对象提供一个指向文本字段的指针;这不应该是IBOutlet
。您可以在init...
方法中执行此操作,或者直接设置它(请注意,这意味着搜索字段必须有到文本字段的连接才能将其传递)到Customer
,这可能是理想的,也可能不是。):*顺便说一句,Obj-C 中的传统类名以大写字母开头:“Customer”,而不是“customer”。
There's no way to get the
NSEvent
s that have already been handed to a view whose code you don't have. If you want to change the key-handling behavior of a view or control, you have to subclass. You might be able to insert another object before the search field in the responder chain, get the event, do whatever you want, and then act like you didn't handle it and pass it on to the field...just a wild thought from the top of my head.UPDATE in reply to your
keyUp:
code:Okay, the problem is becoming clear. When you create that new
customer
* object in code, you also have to hook up its connections in code. They're not connected for you. You don't actually useIBOutlets
for this, just regular old ivars.IBOutlets
are only there so that you can connect objects in the graphical interface of Interface Builder. I don't know exactly what you're trying to do insearcht:
, but just as an example, if you wanted to change the contents of a text field:Where customer.h looks something like:
This will do nothing, including give no warning, because
myTextField
isnil
. You can call methods onnil
all day long and nothing will happen. What you need to do is give thecustomer
object a pointer to the text field; this should not be anIBOutlet
. You could do this in aninit...
method, or just by setting it directly (note that this means that the search field will have to have a connection to the text field in order to pass it on to theCustomer
, which may or may not be desirable):*As an aside, conventionally class names in Obj-C start with uppercase letters: "Customer", not "customer".