@selector有多个参数

coc*_*ner 1 macos cocoa nsmenu nsmenuitem

首先我的代码:

   - (NSMenu*)sourceList:(PXSourceList*)aSourceList menuForEvent:(NSEvent*)theEvent item:(id)item
    {
     if ([theEvent type] == NSRightMouseDown || ([theEvent type] == NSLeftMouseDown && ([theEvent modifierFlags] & NSControlKeyMask) == NSControlKeyMask)) {
      NSMenu * m = [[NSMenu alloc] init];  
      if (item != nil) {
       NSLog(@"%@",[item title]);

       [m addItemWithTitle:[item title] action:@selector(press:) keyEquivalent:@""]; // problem. i want to give "item" as an argument.....

       for (NSMenuItem* i in [m itemArray]) {
        [i setTarget:self];
       }
      } else {
       [m addItemWithTitle:@"clicked outside" action:nil keyEquivalent:@""];
      }
      return [m autorelease];
     }
     return nil;
    }
-(void) press:(id)sender{
 NSLog(@"PRESS");
}
Run Code Online (Sandbox Code Playgroud)

我想用选择器item作为我的press:方法的参数.

非常感谢你 :)

PS:我正在为Mac而不是iPhone做这个.

gra*_*rks 6

NSMenuItem有一个名为的方法setRepresentedObject:,菜单项对象将作为sender参数传递给press:方法.

所以,你需要调整你的代码来调用setRepresentedObject:item与每个去NSMenuItem,然后press:你可以打电话[sender representedObject]来获取项背.