gpt4 book ai didi

swift - NSComboBox 未在下拉列表中显示当前选定的值

转载 作者:搜寻专家 更新时间:2023-11-01 06:30:42 26 4
gpt4 key购买 nike

对 Mac 应用程序编程如此陌生,我遇到了一个问题。在我的 NSComboBoxes 上,它们在重新打开下拉列表时不会突出显示当前选定的项目。我可能只是瞎了眼,或者默认的突出显示颜色是透明的。我只是不知道。请参阅下图了解我得到的内容。

Dropdown Image

此时我只写了很少的代码,一切都在我的 Storyboard 上完成。只是有点困惑,为什么它没有做一些应该很简单的事情。我想我在 iOS 领域花了太多时间。

因此决定将整个项目重新创建为一个单一的 NSCombobox 示例,以证明在运行 Xcode 9.2 时这对我来说很简单。

Sample Project Settings

我连接的唯一导出是 NSComboBox 的委托(delegate)和数据源。 NSComboBox 上唯一更改的设置是启用数据源,并将行为更改为可选。运行此组合的代码非常简单:

import Cocoa

class ViewController: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource {
@IBOutlet weak var combo: NSComboBox!
var dataSource: [String] = ["Off"]

override func viewDidLoad() {
super.viewDidLoad()

for index in 1...512 {
dataSource.append("\(index)")
}
combo.reloadData()
combo.selectItem(at: 0)
}

func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
return dataSource[index]
}

func numberOfItems(in comboBox: NSComboBox) -> Int {
return dataSource.count
}
}

根据此处的 Apple 文档,下拉列表应显示所选项目:Apple NSComboBox Docs

最佳答案

列表会在您离开的地方打开。实现 NSComboBoxDataSource 方法

optional func comboBox(_ comboBox: NSComboBox, indexOfItemWithStringValue string: String) -> Int

让组合框知道要选择哪一行。

Return Value

The index for the item that matches the specified string, or NSNotFound if no item matches.

Discussion

An NSComboBox object uses this method to synchronize the pop-up list’s selected item with the text field’s contents. If you don’t implement this method the receiver does not synchronize the pop-up list’s selected item with the text field’s contents.

关于swift - NSComboBox 未在下拉列表中显示当前选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47802268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com