0

I'm working on a Preference Window for Mac OS and even though the toolbar item is marked as selectable and the delegate method returns the controller identifier for toolbarAllowedItemIdentifiers and toolbarSelectableItemIdentifiers the button remains not selectable.

If I enable it programmatically (hack) in viewDidLoad it just goes back to disabled right away.

Storyboard:

enter image description here

Corresponding Class:

class PreferenceWindowController: NSWindowController, NSToolbarDelegate {

  @IBOutlet weak var toolbar: NSToolbar!

  enum Identifier: String, Equatable {
  case General = "General"
  case Network = "Network"
  case Advanced = "Advanced"

    static let allValuesRaw = [General.rawValue, Network.rawValue]//, Advanced.rawValue]
    static let allValues = [General.toolbarId, Network.toolbarId]//, Advanced.toolbarId]

    var toolbarId: NSToolbarItem.Identifier {
      return NSToolbarItem.Identifier(rawValue: self.rawValue)
    }
  }

  override func windowDidLoad() {
    super.windowDidLoad()

    self.toolbar.selectedItemIdentifier = NSToolbarItem.Identifier(rawValue: Identifier.General.rawValue)
    self.window!.contentViewController = NSStoryboard(name: "Preference", bundle: nil).instantiateController(withIdentifier: "GeneralViewController") as! GeneralViewController
  }




  @IBAction func selectedItem(_ sender: NSToolbarItem) {
    switch sender.itemIdentifier.rawValue {
    case Identifier.General.rawValue:
        self.window!.contentViewController = NSStoryboard(name: "Preference", bundle: nil).instantiateController(withIdentifier: "GeneralViewController") as! GeneralViewController
    case Identifier.Network.rawValue:
        self.window!.contentViewController = NSStoryboard(name: "Preference", bundle: nil).instantiateController(withIdentifier: "NetworkViewController") as! WebhooksViewController
    case Identifier.Advanced.rawValue:
        self.window!.contentViewController = NSStoryboard(name: "Preference", bundle: nil).instantiateController(withIdentifier: "AdvancedViewController") as! AdvancedViewController
    default:

        self.window!.contentViewController = NSStoryboard(name: "Preference", bundle: nil).instantiateController(withIdentifier: "GeneralViewController") as! GeneralViewController
    }
  }


  func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
    return Identifier.allValues
  }

  private func toolbarSelectableItemIdentifiers(_ toolbar: NSToolbar) -> [String] {
    return Identifier.allValuesRaw
  }

}

Update:

I've disabled autovalidate and I've changed all NSToolbarItems be of class CustomToolbarItem, but validate never gets called. The weird thing is that the first button (General) works, while the second one (Network) does not. I've tried it in a new project with the same issue (only the first item works).

    class CustomToolbarItem: NSToolbarItem {

  override func validate() {
    // validate content view
    if
      let control = self.view as? NSControl,
      let action = self.action,
      let validator = NSApp.target(forAction: action, to: self.target, from: self) as AnyObject?
    {
      switch validator {
      case let validator as NSUserInterfaceValidations:
        control.isEnabled = validator.validateUserInterfaceItem(self)
      default:
        control.isEnabled = validator.validateToolbarItem(self)
      }

    } else {
      super.validate()
    }
  }
vale
  • 1,376
  • 11
  • 25

0 Answers0