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

10

我已涉足这个领域有一段时间了...我以为这应该是一个简单的任务,但它并不是 =D

我想做的是,在用户点击组合框时显示组合框列表,而不是特定地在按钮中。

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

7个回答

9
这个答案符合问题标题,但不符合问题本身。Omer想要触摸文本字段并弹出框。
当用户输入文本时,此解决方案显示弹出窗口。
我在Jens Alfke的cocoabuilder上找到了这个答案。我在这里重新发布了他的代码。谢谢Jens。 原始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]; }
  }
}

7
  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)
    

参考 jmoody的回答



3
您可以使用以下代码行:
 [(NSComboBoxCell*)self.acomboBox.cell performSelector:@selector(popUp:)];

3

放置

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:"))
        }
}

2
感谢上面提到的jmoody和Jens Alfke。这是上述解决方案的SWIFT翻译。
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)
    }

 }



}

0

NSComboBox并不是设计成这样工作的。因为用户可能想要编辑控件中的文本,他们需要能够单击它而不会意外地弹出选项。

您需要子类化NSComboBoxCell并更改此行为...但然后您将拥有一个外观标准但行为不标准的控件。如果您决心这样做,请查看NSComboBoxCell的开源版本。有趣的方法似乎是-popUpForComboBoxCell:和friends。


0

根据其他答案,我编写了这个解决方案(在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
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接