Objective c 如何以编程方式打开NSComboBox';谁的名单?
我已经在这附近呆了一段时间了。。我认为这应该是一项简单的任务,但它不是 我想做的是,当用户单击组合框时,显示组合框的列表,但不是特别在按钮中 有什么想法吗?Objective c 如何以编程方式打开NSComboBox';谁的名单?,objective-c,cocoa,nscombobox,Objective C,Cocoa,Nscombobox,我已经在这附近呆了一段时间了。。我认为这应该是一项简单的任务,但它不是 我想做的是,当用户单击组合框时,显示组合框的列表,但不是特别在按钮中 有什么想法吗? 提前谢谢 NSCOMBOX不是设计成这样工作的。因为用户可能想要编辑控件中的文本,所以他们需要能够在不意外弹出选项的情况下单击它 您需要将nsComboxCell子类化并更改此行为。。。但是你会有一个标准外观的控件,它不会以标准的方式运行。如果您决定这样做,请查看的开源版本。有趣的方法似乎是-popupforComboxCell:和frie
提前谢谢 NSCOMBOX不是设计成这样工作的。因为用户可能想要编辑控件中的文本,所以他们需要能够在不意外弹出选项的情况下单击它
您需要将nsComboxCell子类化并更改此行为。。。但是你会有一个标准外观的控件,它不会以标准的方式运行。如果您决定这样做,请查看的开源版本。有趣的方法似乎是-popupforComboxCell:和friends.这个答案符合问题的标题,但不符合问题本身。Omer想触摸文本字段并弹出框 此解决方案在用户输入文本时显示弹出窗口 我从cocoabuilder上找到了这个答案。我把他的密码转寄到这里。谢谢你,简 我在
控制文本didchange:
方法中使用了此代码
- (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]; }
}
}
您可以使用以下代码行:
[(NSComboBoxCell*)self.acomboBox.cell performSelector:@selector(popUp:)];
感谢上面提到的jmoody和Jens Alfke。下面是上述解决方案的快速翻译
import Cocoa
类别ccomboxex: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)
}
}
}
放
进入
这就是我的结局。谢谢@Ahmed Lotfy
这是完整的代码,它在OSX 10.11上对我有效
override func controlTextDidChange(obj: NSNotification) {
if let comboBoxCell = self.comboBox.cell as? NSComboBoxCell {
comboBoxCell.performSelector(Selector("popUp:"))
}
}
true
comboBox.cell?.isAccessibilityExpanded() ?? false
comboBox.cell?.setAccessibilityExpanded(true)
comboBox.cell?.setAccessibilityExpanded(false)
comboBox.cell?.setAccessibilityExpanded(true)
comboBox.cell?.setAccessibilityExpanded(false)
参考..基于我编写的其他答案(使用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
}
}
}
// 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
}
}
}