带有多个参数的@selector

发布于 2024-10-12 00:43:31 字数 970 浏览 2 评论 0原文

首先是我的代码:

   - (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");
}

我想将 item 作为带有选择器的 press: 方法的参数。

非常感谢:)

PS: 我是在 Mac 上做这个的,而不是在 iPhone 上。

first my code:

   - (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");
}

I want to give item as an argument to my press: method with a selector.

Thank you very much :)

PS: I'm doing this for the mac not iPhone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳男子 2024-10-19 00:43:31

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

因此,您需要调整代码以使用每个 NSMenuItem 附带的 item 调用 setRepresentedObject:,然后在 press:< /code> 您可以调用 [senderrepresentedObject] 来取回该项目。

NSMenuItem has a method called setRepresentedObject:, and the menu item object will be passed as the sender parameter to press: method.

So you need to adjust your code to call setRepresentedObject: with the item that goes with each NSMenuItem, and then in press: you can call [sender representedObject] to get that item back.

断肠人 2024-10-19 00:43:31

我几乎肯定 @selector(press:) 消息中包含的“sender”参数是 NSMenuItem

所以:

- (void) press:(id)sender {
  NSLog(@"sender: %@", sender);
}

这应该记录发送者是所选的 NSMenuItem

edit 误解了问题...

您希望在选择某个 menuItem 时检索 item 对象。这很容易。只需执行以下操作:

NSMenuItem * menuItem = [m addItemWithTitle:[item title] action:@selector(press:) keyEquivalent:@""];
[menuItem setTarget:self];
[menuItem setRepresentedObject:item];

然后在您的 press: 方法中...

- (void) press:(id)sender {
  //sender is the NSMenuItem
  id selectedItem = [sender representedObject];
}

I'm almost positive that the "sender" parameter that is included with the @selector(press:) message is the NSMenuItem.

So:

- (void) press:(id)sender {
  NSLog(@"sender: %@", sender);
}

That should log that the sender is the NSMenuItem that was selected.

edit misinterpreted the question...

You want to retrieve the item object when a certain menuItem is selected. That's easy. Just do:

NSMenuItem * menuItem = [m addItemWithTitle:[item title] action:@selector(press:) keyEquivalent:@""];
[menuItem setTarget:self];
[menuItem setRepresentedObject:item];

Then in your press: method...

- (void) press:(id)sender {
  //sender is the NSMenuItem
  id selectedItem = [sender representedObject];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文