1
votes

Here is what I'm trying to achieve.

Under submenu 1, there are three options to choose from: Default Action, Action 1, and Action 2. I would like to do something similar to Radio Button functionality, where if one option is selected, the other one is automatically unchecked.

I was wondering if there was any way to execute NSOffState or NSOnState of other IBAction func within other IBAction func. An example would be a menu where the user has to choose between Beginner, Intermediate or Advanced modes when launching a new game.

e.g.

@IBAction func actionOne(sender: NSMenuItem){

   if(sender.state == NSOnState){
      sender.state = NSOffState
      /*turn on Default Action*/
   } else {
      sender.state = NSOnState
      /*turn off Default Action and Action 2*/
      /*code for Action 1's settings*/
   }

@IBAction func actionTwo(sender: NSMenuItem){

   if(sender.state == NSOnState){
      sender.state = NSOffState
      /*turn on Default Action*/
   } else {
      sender.state = NSOnState
      /*turn off Default Action and Action 1*/
      /*code for Action 1's settings*/
   }

@IBAction func defaultAction(sender: NSMenuItem){

   if(sender.state == NSOnState){
      sender.state = NSOffState
      /*code for default settings*/
   } else {
      sender.state = NSOnState
      /*turn off Action 1 or Action 2 (whichever one was on)*/
      /*code for default settings*/
   }

And apparently I can't post any images due to lack of reputation. I'll link it to a dropbox image; https://goo.gl/SGvvcq

Here is a link to the Apple's official page for the description of setState. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MenuList/Articles/MenuItemStates.html

"You can use states to implement a group of mutually exclusive menu items, much like a group of radio buttons. For example, a game could have three menu items to show the level of play: Beginner, Intermediate, and Advanced. To implement a such a group, create one action message that they all use. This action message changes the appropriate setting, and then reflects that change by unchecking the currently checked item and checking the newly selected item."

1

1 Answers

1
votes

There's many ways to do this. Here's an example of an easy one:

@IBOutlet weak var testsMenu: NSMenu!

func actionCommonToAllMenus(#current: NSMenuItem) {
    // Loops over the array of menu items
    for menuItem in testsMenu.itemArray as! [NSMenuItem] {
        // Switches off the first (and unique) 'on' item
        if menuItem.state == NSOnState {
            menuItem.state = NSOffState
            break
        }
    }
    // Previous 'on' item is now 'off', time to set the current item to 'on'
    current.state = NSOnState
}

@IBAction func actionMenuOne(sender: NSMenuItem) {
    actionCommonToAllMenus(current: sender)
    // do menu 1 stuff
}

@IBAction func actionMenuTwo(sender: NSMenuItem) {
    actionCommonToAllMenus(current: sender)
    // do menu 2 stuff
}

By creating an action common to all menus, you can avoid having to put control code inside each menu action, you just have to call your actionCommonToAllMenus method.