你如何添加上下文感觉菜单到NSOutlineView(即右键菜单) [英] How do you add context senstive menu to NSOutlineView (ie right click menu)
问题描述
如何添加在NSOutlineView中右键单击一行的功能,以便可以说删除对象或其他活动。 (即当你在Apple Mail应用程序中右键单击一个文件夹时)
How do you add the ability to right click on a row in an NSOutlineView so you can say delete an object or some other activity. (ie Like when you right click on a folder in the Apple Mail app)
我想我有一半,我有一个NSOutlineView的子类,右键单击并显示基于所选行而不是鼠标点击的行的上下文菜单。
I think I am half way there, I have a subclass of NSOutlineView that allows me to catch the right click and display a context menu based on the selected row rather than the row the mouse is clicking on.
@implementation NSContextOutlineView
- (NSMenu *)defaultMenu {
if([self selectedRow] < 0) return nil;
NSMenu *theMenu = [[[NSMenu alloc] initWithTitle:@"Model browser context menu"] autorelease];
[theMenu insertItemWithTitle:@"Add package" action:@selector(addSite:) keyEquivalent:@"" atIndex:0];
NSString* deleteItem = [NSString stringWithFormat: @"Remove '%i'", [self selectedRow]];
[theMenu insertItemWithTitle: deleteItem action:@selector(removeSite:) keyEquivalent:@"" atIndex:1];
return theMenu;
}
- (NSMenu *)menuForEvent:(NSEvent *)theEvent {
return [self defaultMenu];
}
@end
对不起,如果答案很明显我只是无法找到在线或在文档中有任何帮助。
Sorry if the answer is obvious I just cant find any help on this online or in the documentation.
感谢Void的答案,它让我使用这个:
Thanks to Void for the answer, it lead me to using this:
- (NSMenu *)menuForEvent:(NSEvent *)theEvent {
NSPoint pt = [self convertPoint:[theEvent locationInWindow] fromView:nil];
id item = [self itemAtRow: [self rowAtPoint:pt]];
return [self defaultMenuFor: item];
}
推荐答案
找出点击发生在哪一行。您可以将此作为参数传递到 defaultMenu 方法中 - 也可以调用 defaultMenuForRow:
In your menuForEvent method you can find out which row the click occurred on. You can pass that as a parameter to your defaultMenu method -- maybe call it defaultMenuForRow:
-(NSMenu*)menuForEvent:(NSEvent*)evt
{
NSPoint pt = [self convertPoint:[evt locationInWindow] fromView:nil];
int row=[self rowAtPoint:pt];
return [self defaultMenuForRow:row];
}
现在,您可以为事件中找到的行构建菜单。 。
Now you can build the menu for the row you found in the event...
-(NSMenu*)defaultMenuForRow:(int)row
{
if (row < 0) return nil;
NSMenu *theMenu = [[[NSMenu alloc]
initWithTitle:@"Model browser context menu"]
autorelease];
[theMenu insertItemWithTitle:@"Add package"
action:@selector(addSite:)
keyEquivalent:@""
atIndex:0];
[theMenu insertItemWithTitle:[NSString stringWithFormat:@"Remove '%i'", row]
action:@selector(removeSite:)
keyEquivalent:@""
atIndex:0];
// you'll need to find a way of getting the information about the
// row that is to be removed to the removeSite method
// assuming that an ivar 'contextRow' is used for this
contextRow = row;
return theMenu;
}
此外,正如评论中已经提到的,你真的不应该使用NS-prefix在你自己的类上。在未来会有冲突的可能性,它会混淆正在看你的代码的每个人 - 包括你自己:)
Also, as already mentioned in the comments, you really shouldn't use the NS-prefix on your own classes. There is a potential for a clash in the future plus it will confuse everybody that is looking at your code - including yourself :)
希望这有助于...
这篇关于你如何添加上下文感觉菜单到NSOutlineView(即右键菜单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!