-1

I am writing a Swift Application in xCode and have no problem creating a new menuitem and wiring it up so that the sent action responds to a function in my ViewController. Where I am having difficulty is if I try to override one of the existing Apple, preset menu actions. For example, I would like to display my own help so I want to override the showHelp: function that is currently the action set for the help menu.

This what I did. Remember, I am using a storyboard and this worked for new menu items.

  1. Selected the Attributes inspector for the FirstResponder of the MainMenu.xib
  2. Added a new action showMyHelp: and I left the type as id
  3. Created a new function called showMyHelp() in my ViewController
  4. Selected the myApplicationName help menuitem from the main menu and then from the connections panel, dragged from Sent Actions to the first responder of the MainMenu and selected showMyHelp from the selection list. The Sent Actions now reads action > First Responder / showMyHelp:

However, when I run the app, the help menu is now disabled and I cannot access it.

Again, if i repeat this method for one of my custom menuItems that I have added, it works fine. The menuItem is not disabled and the action is performed.

Stewart Lynch
  • 875
  • 9
  • 26

3 Answers3

0

OK. I discovered what my problem was. In step 3 above, I needed to include the sender: AnyObject? as a parameter when calling the function so showMyHelp(sender: AnyObject?)

All is well now. Next step is to figure out how to manage enabling and disabling menu items as required.

Stewart Lynch
  • 875
  • 9
  • 26
  • To future readers, I had to make it `someFunc(_ sender: AnyObject?)` to get the menu item to not be greyed out – Luka Kerr Oct 10 '17 at 07:49
0

Thank you for your helpful post. However, i find a little thing to make it working:

  • Function signature should start with @IBAction
@IBAction func showMyHelp(_ sender: AnyObject?)
  • This function should be defined in your AppDelegate.

For the next, follow the previous steps and it works ! :)

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Tchoupinax
  • 235
  • 3
  • 12
0

You can also implement on the ViewController.
But, First you indicate flag for accept to first responder.

import Cocoa

class ViewController: NSViewController {
  // ..
  override var acceptsFirstResponder: Bool {
    return true
  }

  @IBAction func showHelp(_ sender: Any) {
    print("showHelp")
  }
Namo
  • 456
  • 5
  • 11