0

I have a NSViewController with multiple NSComboBox instances.

I have the following code that adds the items to the NSComboBox and then selects an item:

@IBOutlet weak var monitors: NSComboBox! 


fileprivate func fillMonitors()
{
    self.monitors.removeAllItems()

    var items: [String] = []
        
    for screen in NSScreen.screens
    {
      items.append(screen.localizedName)
    }
        
    self.monitors.addItems(withObjectValues: items)
        
    guard let primaryMonitor = self.configuration?.layout.displayName ?? NSScreen.main?.localizedName else { return }
        
     if let index = items.firstIndex(of: primaryMonitor)
     {
       self.monitors.selectItem(at: index)
     }
}

The items are added and the correct monitor is found which means that the following line selects the proper monitor:

self.monitors.selectItem(at: index)

However, the NSComboBox does not display any text whatsoever. Also, the opening of the dropdown feels very weird, sometimes it's closing again, but then it works properly.

When selecting another item, the event handler is called and the value is processed, so I don't think that this is a bug in my code. Has anyone experienced this before? I have tried to set the text directly using the stringValue but it also does not work. I am really lost here. Really appreciate any hints. I am not using a datasource (property set to false), the delegate is nil.

Using Ventura with Xcode 14.1 RC2.

inexcitus
  • 2,471
  • 2
  • 26
  • 41
  • 1
    I just built this code. I had to remove self.configuration?... but after that it works fine. the combobox shows all my monitors and shows the primary. Also on 14.1. – john elemans Nov 02 '22 at 15:09
  • Is the text also displayed when calling selectItem(at: 0)? The combo box contains the screens as well, but when selecting one or calling selectItem the text just never appears. – inexcitus Nov 02 '22 at 16:31
  • Yes, it correctly shows the first monitor (at 0). – john elemans Nov 02 '22 at 21:06
  • I removed the comboboxes and added new ones. Now it seems to work. Strange. – inexcitus Nov 03 '22 at 03:23

0 Answers0