简体   繁体   English

如何将参数传递给选择器?

[英]How can I pass a parameter to a selector?

I have an NSSearchField: 我有一个NSSearchField:

[searchField setAction:@selector(doSearchWithQuery:)];

Here is my doSearchQuery: 这是我的doSearchQuery:

-(void)doSearchWithQuery:(NSString*)query{

How can I pass the contents of my searchfield into doSearchWithQuery? 如何将搜索字段的内容传递给doSearchWithQuery?

You can't do exactly what you're describing. 你无法完全按照自己的描述去做。 A selector doesn't do anything or accept any parameters — it's just the name of the message to send. 选择器不执行任何操作或接受任何参数 - 它只是要发送的消息的名称。 You can only pass arguments when you actually send a message. 您只能在实际发送消息时传递参数。 However, controls always pass themselves as an argument to their actions, so what you need is a wrapper method along these lines: 但是,控件总是将自己作为参数传递给自己的动作,所以你需要的是一个包装方法:

- (void)doSearchFromSearchField:(NSSearchField *)sender {
    [self doSearchWithQuery:[sender stringValue]];
}

And set that as the action. 并将其设置为动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM