
我想我一半的地方,我有一个NSOutlineVIEw的子类,让我抓住右键单击,并显示一个上下文菜单基于所选行,而不是鼠标点击的行。
@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 对不起,如果答案很明显,我在这个网上或文档中找不到任何帮助。
感谢VoID的答案,它引导我使用这个:
- (NSMenu *)menuForEvent:(NSEvent *)theEvent { NSPoint pt = [self convertPoint:[theEvent locationInWindow] fromVIEw:nil]; ID item = [self itemAtRow: [self rowAtPoint:pt]]; return [self defaultMenuFor: item];}解决方法 在您的menuForEvent方法中,您可以找到发生点击的行。您可以将其作为参数传递给defaultMenu方法 – 也许称为defaultMenuForRow: -(NSMenu*)menuForEvent:(NSEvent*)evt { NSPoint pt = [self convertPoint:[evt locationInWindow] fromVIEw:nil]; int row=[self rowAtPoint:pt]; return [self defaultMenuForRow:row];} 现在您可以为事件中找到的行构建菜单
-(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前缀。有可能在未来发生冲突加上它会混淆所有人正在看你的代码 – 包括你自己:)
希望这可以帮助…
总结以上是内存溢出为你收集整理的objective-c – 如何向NSOutlineView添加上下文敏感菜单(即右键菜单)全部内容,希望文章能够帮你解决objective-c – 如何向NSOutlineView添加上下文敏感菜单(即右键菜单)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)