How to pass parameter to selector?

I have an NSSearchField:

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

Here is my doSearchQuery:

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

How do I pass the contents of my search field to doSearchWithQuery?

+3
source share
1 answer

You cannot do what you describe. The selector does nothing or does not accept any parameters - it's just the name of the message to send. You can only pass arguments when sending a message. However, controls always pass themselves as arguments to their actions, so you need a wrapper method in the following lines:

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

And set it as an action.

+4
source

Source: https://habr.com/ru/post/1778873/


All Articles