How can I pass a parameter to a selector?

2019-02-18 17:57发布

I have an NSSearchField:

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

Here is my doSearchQuery:

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

How can I pass the contents of my searchfield into doSearchWithQuery?

1条回答
迷人小祖宗
2楼-- · 2019-02-18 18:33

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.

查看更多
登录 后发表回答