Mac:取消选中子菜单项 [英] Mac: Uncheck sub-menu item

查看:119
本文介绍了Mac:取消选中子菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要实现的目标.

Here is what I'm trying to achieve.

在子菜单 1 下,有三个选项可供选择:默认操作、操作 1 和操作 2.我想做一些类似于单选按钮功能的操作,如果选择了一个选项,则自动取消选中另一个选项.

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.

我想知道是否有任何方法可以在其他 IBAction 函数中执行其他 IBAction 函数的 NSOffState 或 NSOnState.例如,用户在启动新游戏时必须在初级、中级或高级模式之间进行选择的菜单.

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.

例如

@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*/
   }

显然,由于缺乏声誉,我无法发布任何图片.我会把它链接到一个保管箱图像;https://goo.gl/SGvvcq

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

这里有一个指向苹果官方页面的链接,用于描述 setState.https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MenuList/Articles/MenuItemStates.html

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."

推荐答案

有很多方法可以做到这一点.这是一个简单的例子:

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
}

通过创建所有菜单通用的操作,您可以避免在每个菜单操作中放置控制代码,您只需调用您的 actionCommonToAllMenus 方法.

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.

这篇关于Mac:取消选中子菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆