Cocoa Selector Question

别来无恙 提交于 2019-12-12 16:20:07

问题


I have a question on how to use a selector with multiple parameters. I need to toggle this:

-(void)openBackupNamed:(NSString *)name

using this:

[backupList addItemWithTitle:file action:@selector(openBackupNamed:) keyEquivalent:@""];

I know that there is the withObject: parameter for these cases, but I can't do this in the addItemWithTitle:action:keyEquivalent: method or am I missing something?

Thanks


回答1:


In your case you will have to create a new NSInvocation object and set it's index 2 parameter to your NSString (The 0- and 1-indexed parameters are reserved).

Example:

// Assuming:
NSString *myString = ...;

/* / */

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(openBackupNamed:)]];
[invocation setSelector:@selector(openBackupNamed:)];
[invocation setTarget:self];
[invocation setArgument:&myString atIndex: 2];

[invocation invoke]; // or use invokeWithTarget: instead of the above setTarget method.

Read the ADC NSInvocation Class Reference

Please mind the setArgument message. You have to pass it the address of your parameter (your string), not the actual object itself.



来源:https://stackoverflow.com/questions/3076825/cocoa-selector-question

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