osx - Mac: Uncheck sub-menu item -


here i'm trying achieve.

under submenu 1, there 3 options choose from: default action, action 1, , action 2. similar radio button functionality, if 1 option selected, other 1 automatically unchecked.

i wondering if there way execute nsoffstate or nsonstate of other ibaction func within other ibaction func. example menu user has choose between beginner, intermediate or advanced modes when launching 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 , action 2*/       /*code 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 , action 1*/       /*code action 1's settings*/    }  @ibaction func defaultaction(sender: nsmenuitem){     if(sender.state == nsonstate){       sender.state = nsoffstate       /*code default settings*/    } else {       sender.state = nsonstate       /*turn off action 1 or action 2 (whichever 1 on)*/       /*code default settings*/    } 

and apparently can't post images due lack of reputation. i'll link dropbox image; https://goo.gl/sgvvcq

here link apple's official page description of setstate. https://developer.apple.com/library/mac/documentation/cocoa/conceptual/menulist/articles/menuitemstates.html

"you can use states implement group of mutually exclusive menu items, group of radio buttons. example, game have 3 menu items show level of play: beginner, intermediate, , advanced. implement such group, create 1 action message use. action message changes appropriate setting, , reflects change unchecking checked item , checking newly selected item."

there's many ways this. here's example of easy one:

@iboutlet weak var testsmenu: nsmenu!  func actioncommontoallmenus(#current: nsmenuitem) {     // loops on array of menu items     menuitem in testsmenu.itemarray as! [nsmenuitem] {         // switches off first (and unique) 'on' item         if menuitem.state == nsonstate {             menuitem.state = nsoffstate             break         }     }     // previous 'on' item 'off', time set current item 'on'     current.state = nsonstate }  @ibaction func actionmenuone(sender: nsmenuitem) {     actioncommontoallmenus(current: sender)     // menu 1 stuff }  @ibaction func actionmenutwo(sender: nsmenuitem) {     actioncommontoallmenus(current: sender)     // menu 2 stuff } 

by creating action common menus, can avoid having put control code inside each menu action, have call actioncommontoallmenus method.


Comments