How can I pass a parameter to a selector?

怎甘沉沦 提交于 2019-12-01 06:40:53

问题


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:


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.



来源:https://stackoverflow.com/questions/4372468/how-can-i-pass-a-parameter-to-a-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!