Olo*_*lof 6 cocoa contextmenu objective-c nsmenu nsoutlineview
我有一个带有源列表(NSOutlineView)的窗口.我的源列表只有两个级别.第一级是标题,第二级是数据.我想在一些数据单元格上有一个上下文菜单.不是全部.
首先,我尝试在表格单元格视图上附加一个表示数据单元格的菜单 - >没有任何反应.
其次,我在IB中的大纲视图上附加一个菜单 - >在每个单元格(标题和数据)上打开上下文菜单.我搜索停止打开菜单,但我找不到任何东西.
你有什么想法吗?
谢谢
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;
}
Run Code Online (Sandbox Code Playgroud)
这假设我们正在使用ARC进行编译,因此您无需自动释放正在创建的菜单对象.
| 归档时间: |
|
| 查看次数: |
1315 次 |
| 最近记录: |