开发者

Cocoa Selector Question

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

-(void开发者_StackOverflow)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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜