0

I am creating a Mac app to read a XML document and save it. Everything is working fine except "Revert To" & "Duplicate" menu items. Till i find a solution for that i want to disable both of them, but i didn't found any solution for it, Please let me know how can i disable both the options so that they end user cannot click on them.

I already looked into Menu's from .xib so that i can disable them but i don't see any options.

I tried to somehow manipulate below code, but i didn't found any answers.

override func duplicate() throws -> NSDocument { return self }

Shiva Kumar
  • 389
  • 3
  • 22

1 Answers1

2

The general way to disable a menu item in Cocoa is returning false in validateMenuItem(_:) (or validateUserInterfaceItem(_:).)

In this case, put the following code in your NSDocument subclass.

override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {

    guard let action = menuItem.action else { return false }

    switch action {
    case #selector(duplicate(_:)):
        return false
    case #selector(revertToSaved(_:)):
        return false
    default: break
    }

    return super.validateMenuItem(menuItem)
}

However, according to the Apple's Human Interface Guidelines, you should not leave menu items which are not used. So, if your app doesn't support duplication and revert features at all, I prefer to remove the items rather than to disable.

1024jp
  • 2,058
  • 1
  • 16
  • 25
  • thanks for your answer, it did helped me. I am trying to give validateMenuItem in appdelegate which didn't worked, but moving the code to NSDocument subclass as you mentioned did helped. – Shiva Kumar Dec 28 '17 at 06:38
  • @ShivaKumar, i met the same problem, i can't find the menu item "Duplicate" in the xib/storyboard. Do you know how to remove it ? – jimwan Jul 09 '18 at 06:48
  • @jimwan, no i don't know how to remove it but above answer makes it disable only. – Shiva Kumar Jul 17 '18 at 11:09