当段被点击时,显示NS​​SegmentedControl菜单,尽管已设置操作 [英] Show NSSegmentedControl menu when segment clicked, despite having set action

查看:247
本文介绍了当段被点击时,显示NS​​SegmentedControl菜单,尽管已设置操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSSegmentedControl在我的UI上有4个按钮。控件连接到一个方法,该方法将根据单击哪个段调用不同的方法:

I have an NSSegmentedControl on my UI with 4 buttons. The control is connected to a method that will call different methods depending on which segment is clicked:

- (IBAction)performActionFromClick:(id)sender {
    NSInteger selectedSegment = [sender selectedSegment];
    NSInteger clickedSegmentTag = [[sender cell] tagForSegment:selectedSegment];

    switch (clickedSegmentTag) {
            case 0: [self showNewEventWindow:nil]; break;
            case 1: [self showNewTaskWindow:nil]; break;
            case 2: [self toggleTaskSplitView:nil]; break;
            case 3: [self showGearMenu]; break;
    }
}

段4有一个菜单awakeFromNib方法。当用户点击细分时,我想要此菜单下拉。在这一点上,只有当用户点击&按住菜单。从我在线研究这是因为连接的行动。

Segment 4 has has a menu attached to it in the awakeFromNib method. I'd like this menu to drop down when the user clicks the segment. At this point, it only will drop if the user clicks & holds down on the menu. From my research online this is because of the connected action.

我目前正在使用一些代码来获取段控制的原点,并使用NSMenu的 popUpContextMenu弹出上下文菜单:withEvent:forView ,但是这与菜单下拉到分段控制单元格下方的标准行为相比,这是一个相当黑的,看起来不错。

I'm presently working around it by using some code to get the origin point of the segment control and popping up the context menu using NSMenu's popUpContextMenu:withEvent:forView but this is pretty hacktastic and looks bad compared to the standard behavior of having the menu drop down below the segmented control cell.

有一种方法,我可以让菜单下拉,因为它应该在单击后,而不是做上下文菜单的事情?

Is there a way I can have the menu drop down as it should after a single click rather than doing the hacky context menu thing?

推荐答案

我不确定任何内置的方法来做这个(虽然它真的是一个闪亮的洞NSSegmentedControl API)。

I'm not sure of any built-in way to do this (though it really is a glaring hole in the NSSegmentedControl API).

我的建议是继续做你正在做的事情弹出上下文菜单。但是,您可以通过执行以下操作,而不是仅使用分段控件的原点:

My recommendation is to continue doing what you're doing popping up the context menu. However, instead of just using the segmented control's origin, you could position it directly under the segment (like you want) by doing the following:

NSPoint menuOrigin = [segmentedControl frame].origin;
menuOrigin.x = NSMaxX([segmentedControl frame]) - [segmentedControl widthForSegment:4];
// Use menuOrigin where you _were_ just using [segmentedControl frame].origin

完美或理想,但它应该完成工作,并给出你的用户期望的外观/行为。

It's not perfect or ideal, but it should get the job done and give the appearance/behavior your users expect.

(另外,NSSegmentedControl 真的需要 -rectForSegment:方法)

(as an aside, NSSegmentedControl really needs a -rectForSegment: method)

这篇关于当段被点击时,显示NS​​SegmentedControl菜单,尽管已设置操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆