如何将参数传递给选择器? [英] How can I pass a parameter to a selector?

查看:144
本文介绍了如何将参数传递给选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSSearchField:

I have an NSSearchField:

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

这里是我的doSearchQuery:

Here is my doSearchQuery:

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



我可以将我的searchfield的内容传递给doSearchWithQuery?

How can I pass the contents of my searchfield into 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]];
}

并设置为操作。

这篇关于如何将参数传递给选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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