问题描述
我有一个关于如何使用带有多个参数的选择器的问题。我需要切换这个:
I have a question on how to use a selector with multiple parameters. I need to toggle this:
使用:
[backupList addItemWithTitle:file action:@selector(openBackupNamed:) keyEquivalent:@""];
我知道有 withObject:
参数,但是我不能在 addItemWithTitle:action: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?
感谢
推荐答案
在你的情况下,你必须创建一个新的NSInvocation对象,您的NSString(保留0和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).
示例:
// 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.
阅读ADC
请注意 setArgument
消息。您必须传递参数(字符串)的地址,而不是实际的对象本身。
Please mind the setArgument
message. You have to pass it the address of your parameter (your string), not the actual object itself.
这篇关于可可选择器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!