Dr.*_*eon 16 macos cocoa objective-c
我该怎么办呢?
我在考虑......
[NSMenu popUpContextMenu:menu withEvent:event forView:(NSButton *)sender];
Run Code Online (Sandbox Code Playgroud)
aru*_*n.s 29
对.
按钮动作调用
[NSMenu popUpContextMenu:menu withEvent:event forView:(NSButton *)sender];
Run Code Online (Sandbox Code Playgroud)
哪里
menu
:你想要显示的菜单 sender
:你点击的按钮event
:NSEvent
你创造的新事物创建新内容时NSEvent
,请指定要在何处显示弹出菜单的位置.
Kau*_*eya 11
接受答案的Swift版本
@IBAction func actionOccurred(sender: NSButton) {
if let event = NSApplication.sharedApplication().currentEvent {
NSMenu.popUpContextMenu(sender.menu!, withEvent: event, forView: sender)
}
}
Run Code Online (Sandbox Code Playgroud)
正如我所评论的那样,我发现ButtonMadness的例子并不完美.我的实现似乎更好.鼠标按下时显示菜单,整个按钮保持按下状态,可以指定菜单位置,并且菜单将被解除,而不会出现虚假显示.
说实话,在大多数情况下,NSPopupButton是更好的选择.我使用这段代码主要是因为按钮和弹出窗口有一个类的便利,因为菜单不包含弹出控件图像和标题.我从单独的笔尖加载菜单,并根据需要在应用程序的其他位置重复使用它.
请注意,添加弹出窗口和菜单的附加支持是微不足道的.
NSButton subclass:
- (void)mouseDown:(NSEvent *)theEvent {
// if a menu is defined let the cell handle its display
if (self.menu) {
if ([theEvent type] == NSLeftMouseDown) {
[[self cell] setMenu:[self menu]];
} else {
[[self cell] setMenu:nil];
}
}
[super mouseDown:theEvent];
}
NSButtonCell subclass:
- (BOOL)trackMouse:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp
{
// if menu defined show on left mouse
if ([event type] == NSLeftMouseDown && [self menu]) {
NSPoint result = [controlView convertPoint:NSMakePoint(NSMidX(cellFrame), NSMidY(cellFrame)) toView:nil];
NSEvent *newEvent = [NSEvent mouseEventWithType: [event type]
location: result
modifierFlags: [event modifierFlags]
timestamp: [event timestamp]
windowNumber: [event windowNumber]
context: [event context]
eventNumber: [event eventNumber]
clickCount: [event clickCount]
pressure: [event pressure]];
// need to generate a new event otherwise selection of button
// after menu display fails
[NSMenu popUpContextMenu:[self menu] withEvent:newEvent forView:controlView];
return YES;
}
return [super trackMouse:event inRect:cellFrame ofView:controlView untilMouseUp:untilMouseUp];
}
Run Code Online (Sandbox Code Playgroud)
最近,我试图实现它,我想,我用一个更简单的解决方案
-(IBAction)buttonClick:(id)sender {
NSButton * b = (NSButton*)sender;
NSPoint l = [ self.window convertBaseToScreen:b.frame.origin ];
[ self.menu popUpMenuPositioningItem:nil atLocation:l inView:nil ];
}
Run Code Online (Sandbox Code Playgroud)
更新
convertBaseToScreen
从10.7开始不推荐使用,而不是以convertRectToScreen
下列方式使用:
NSPoint l = [self.window convertRectToScreen:b.frame].origin;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10970 次 |
最近记录: |