如何以编程方式打开 NSComboBox 列表?

发布于 2024-10-08 18:46:05 字数 110 浏览 1 评论 0原文

我已经解决这个问题有一段时间了..我认为这应该是一个简单的任务,但它不是 =D

我想做的是,当用户单击组合框时显示组合框的列表,但不是专门在按钮。

有什么想法吗? 提前致谢!

I've been around this for a while.. I thought this should be an easy task, but it isn't =D

What I am trying to do, is to display the combobox's list when the user clicks the combobox but not specifically in the button.

Any Idea?
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夏花。依旧 2024-10-15 18:46:06

您可以使用以下代码行:

 [(NSComboBoxCell*)self.acomboBox.cell performSelector:@selector(popUp:)];

You can use the following code line:

 [(NSComboBoxCell*)self.acomboBox.cell performSelector:@selector(popUp:)];
蓝礼 2024-10-15 18:46:06

放入

comboBoxCell.performSelector(Selector("popUp:"))

就是

override func controlTextDidChange(obj: NSNotification) {}

我最终得到的。谢谢@Ahmed Lotfy

这是完整的代码,它在 OSX 10.11 上适用于我

override func controlTextDidChange(obj: NSNotification) {
        if let comboBoxCell = self.comboBox.cell as? NSComboBoxCell {
            comboBoxCell.performSelector(Selector("popUp:"))
        }
}

Put

comboBoxCell.performSelector(Selector("popUp:"))

Into

override func controlTextDidChange(obj: NSNotification) {}

is what I ended up with. Thanks @Ahmed Lotfy

Here's the full code, it works for me on OSX 10.11

override func controlTextDidChange(obj: NSNotification) {
        if let comboBoxCell = self.comboBox.cell as? NSComboBoxCell {
            comboBoxCell.performSelector(Selector("popUp:"))
        }
}
黯然#的苍凉 2024-10-15 18:46:06

感谢上面提到的 jmoody 和 Jens Alfke。以下是上述解决方案的 SWIFT 翻译。

import Cocoa

CComboBoxEx 类:NSComboBox {

override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)
        // Drawing code here.

       }

func isExpanded() -> Bool{

    if let ax:AnyObject? = NSAccessibilityUnignoredDescendant(self) {
        if ax!.accessibilityAttributeValue(NSAccessibilityExpandedAttribute) != nil {
            return true
        }
    }
    return false
}

func setExpanded (bExpanded:Bool) {

    if let ax:AnyObject? = NSAccessibilityUnignoredDescendant(self) {
       ax!.accessibilitySetValue(NSNumber(bool: bExpanded), forAttribute: NSAccessibilityExpandedAttribute)
    }

 }



}

Thanks to jmoody and Jens Alfke mentioned above. Here is a SWIFT translation of the above solution.

import Cocoa

class CComboBoxEx: NSComboBox {

override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)
        // Drawing code here.

       }

func isExpanded() -> Bool{

    if let ax:AnyObject? = NSAccessibilityUnignoredDescendant(self) {
        if ax!.accessibilityAttributeValue(NSAccessibilityExpandedAttribute) != nil {
            return true
        }
    }
    return false
}

func setExpanded (bExpanded:Bool) {

    if let ax:AnyObject? = NSAccessibilityUnignoredDescendant(self) {
       ax!.accessibilitySetValue(NSNumber(bool: bExpanded), forAttribute: NSAccessibilityExpandedAttribute)
    }

 }



}
娇纵 2024-10-15 18:46:06

NSComboBox 的设计初衷并不是以这种方式工作。由于用户可能想要编辑控件中的文本,因此他们需要能够单击它而不会意外弹出选项。

您需要子类化 NSComboBoxCell 并更改此行为...但是您将拥有一个看起来标准的控件,但其行为却不是标准方式。如果您决心这样做,请查看 NSComboBoxCell。有趣的方法似乎是 -popUpForComboBoxCell: 和朋友。

NSComboBox was not designed to work this way. Because the user may want to edit the text in the control, they'll need to be able to click it without unexpectedly popping up the choices.

You would need to subclass NSComboBoxCell and change this behavior ... but then you'd have a standard-looking control that does not behave in a standard way. If you're determined to do this, take a look at the open source version of NSComboBoxCell. The interesting methods appear to be -popUpForComboBoxCell: and friends.

不必了 2024-10-15 18:46:06

基于其他答案,我编写了这个解决方案(使用 Xcode 10.2.1、Swift 5 进行测试)。它使用相同的想法,但更短一些。

// Put this extension for NSComboBox somewhere in your project

import Cocoa

public extension NSComboBox {

    var isExpanded: Bool{
        set {
            cell?.setAccessibilityExpanded(newValue)
        }
        get {
            return cell?.isAccessibilityExpanded() ?? false
        }
    }
}

// Set your corresponding NSViewController as NSComboBoxDelegate 
// in the storyboard and add this piece of code 
// to expand the combobox when the user types

class MyViewController: NSViewController, NSComboBoxDelegate {

    func controlTextDidChange(_ notification: Notification) {
        guard let comboBox = notification.object as? NSComboBox else { return }
        if comboBox.isExpanded == false {
            comboBox.isExpanded = true
        }
    }
}

Based on the other answers I wrote this solution (tested with Xcode 10.2.1, Swift 5). It uses the same ideas but it's a little shorter.

// Put this extension for NSComboBox somewhere in your project

import Cocoa

public extension NSComboBox {

    var isExpanded: Bool{
        set {
            cell?.setAccessibilityExpanded(newValue)
        }
        get {
            return cell?.isAccessibilityExpanded() ?? false
        }
    }
}

// Set your corresponding NSViewController as NSComboBoxDelegate 
// in the storyboard and add this piece of code 
// to expand the combobox when the user types

class MyViewController: NSViewController, NSComboBoxDelegate {

    func controlTextDidChange(_ notification: Notification) {
        guard let comboBox = notification.object as? NSComboBox else { return }
        if comboBox.isExpanded == false {
            comboBox.isExpanded = true
        }
    }
}
你曾走过我的故事 2024-10-15 18:46:05

这个答案符合问题的标题,但不符合问题本身。奥马尔想要触摸文本字段并弹出框。

该解决方案在用户输入文本时显示弹出窗口。

我在 Jens Alfke 的 cocoabuilder 上找到了这个答案。我在这里重新发布了他的代码。谢谢詹斯。

原始 cocoabuilder 帖子:(http ://www.cocoabuilder.com/archive/cocoa)

@interface NSComboBox (MYExpansionAPI)
@property (getter=isExpanded) BOOL expanded;
@end

@implementation NSComboBox (MYExpansionAPI)

- (BOOL) isExpanded
{
    id ax = NSAccessibilityUnignoredDescendant(self);
    return [[ax accessibilityAttributeValue:
                NSAccessibilityExpandedAttribute] boolValue];
}

- (void) setExpanded: (BOOL)expanded
{
    id ax = NSAccessibilityUnignoredDescendant(self);
    [ax accessibilitySetValue: [NSNumber numberWithBool: expanded]
                 forAttribute: NSAccessibilityExpandedAttribute];
}

我在我的 controlTextDidChange: 方法中使用了此代码。

- (void) controlTextDidChange:(NSNotification *) aNotification {
  NSTextField *textField = [aNotification object];
  NSString *value = [textField stringValue];
  NSComboBox *box = [self comboBox];
  if (value == nil || [value length] == 0) {
    if ([box isExpanded]) { [box setExpanded:NO]; }
  } else {
    if (![box isExpanded]) { [box setExpanded:YES]; }
  }
}

This answer fits the title of the question, but not question itself. Omer wanted to touch a text field and have the box popup.

This solution shows the popup when the user enters text.

I found this answer on cocoabuilder from Jens Alfke. I reposted his code here. Thanks Jens.

original cocoabuilder post: (http://www.cocoabuilder.com/archive/cocoa)

@interface NSComboBox (MYExpansionAPI)
@property (getter=isExpanded) BOOL expanded;
@end

@implementation NSComboBox (MYExpansionAPI)

- (BOOL) isExpanded
{
    id ax = NSAccessibilityUnignoredDescendant(self);
    return [[ax accessibilityAttributeValue:
                NSAccessibilityExpandedAttribute] boolValue];
}

- (void) setExpanded: (BOOL)expanded
{
    id ax = NSAccessibilityUnignoredDescendant(self);
    [ax accessibilitySetValue: [NSNumber numberWithBool: expanded]
                 forAttribute: NSAccessibilityExpandedAttribute];
}

I used this code in my controlTextDidChange: method.

- (void) controlTextDidChange:(NSNotification *) aNotification {
  NSTextField *textField = [aNotification object];
  NSString *value = [textField stringValue];
  NSComboBox *box = [self comboBox];
  if (value == nil || [value length] == 0) {
    if ([box isExpanded]) { [box setExpanded:NO]; }
  } else {
    if (![box isExpanded]) { [box setExpanded:YES]; }
  }
}
鱼忆七猫命九 2024-10-15 18:46:05
  1. 如果 NSComboBox 的列表已展开,则返回 true

    comboBox.cell?.isAccessibilityExpanded() ??错误的
    
  2. 打开 NSComboBox 的列表

    comboBox.cell?.setAccessibilityExpanded(true)
    
  3. 关闭 NSComboBox 的列表

    comboBox.cell?.setAccessibilityExpanded(false)
    

Ref. jmoody 的回答

  1. Returns true if the NSComboBox's list is expanded

    comboBox.cell?.isAccessibilityExpanded() ?? false
    
  2. Open the NSComboBox's list

    comboBox.cell?.setAccessibilityExpanded(true)
    
  3. Close the NSComboBox's list

    comboBox.cell?.setAccessibilityExpanded(false)
    

Ref. jmoody’s answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文