gpt4 book ai didi

objective-c - "Source List"中仅某些项目的上下文菜单

转载 作者:太空狗 更新时间:2023-10-30 03:41:57 24 4
gpt4 key购买 nike

我有一个带有源列表 (NSOutlineView) 的窗口。我的源列表只有两个级别。第一层是标题,第二层是数据。我想在某些数据单元格上有一个上下文菜单。不是全部。

首先,我尝试在代表数据单元格的表格单元格 View 上附加一个菜单 -> 没有任何反应。

其次,我在 IB 的大纲 View 上附加了一个菜单 -> 上下文菜单在每个单元格(标题和数据)上打开。我搜索停止打开菜单,但我没有找到任何东西。

你有什么想法吗?

谢谢

OS X 10.8.2 Lion,Xcode 4.5.2,SDK 10.8

最佳答案

如果您将 NSOutlineView 子类化,您可以覆盖 menuForEvent: 以仅当用户单击正确的行时返回菜单。这是一个例子:

- (NSMenu *)menuForEvent:(NSEvent *)event;
{
//The event has the mouse location in window space; convert it to our (the outline view's) space so we can find which row the user clicked on.
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
NSInteger row = [self rowAtPoint:point];

//If the user did not click on a row, or is not exactly one level down from the top level of hierarchy, return nil—that is, no menu.
if ( row == -1 || [self levelForRow:row] != 1 )
return nil;

//Create and populate a menu.
NSMenu *menu = [[NSMenu alloc] init];
NSMenuItem *delete = [menu addItemWithTitle:NSLocalizedString( @"Delete", @"" ) action:@selector(delete:) keyEquivalent:@""];

[self selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];

//Set the Delete menu item's represented object to the clicked-on item. If the user chooses this item, we'll retrieve its represented object so we know what to delete.
[delete setRepresentedObject:[self itemAtRow:row]];

return menu;
}

这假设我们正在使用 ARC 进行编译,因此您不需要自动释放正在创建的菜单对象。

关于objective-c - "Source List"中仅某些项目的上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14447120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com