如何将焦点设置为NSButton? [英] How to set focus to an NSButton?

查看:141
本文介绍了如何将焦点设置为NSButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为OS X Yosemite编写一个小的菜单栏应用程序.单击菜单栏图标时,将显示一个弹出窗口,其中带有许多嵌入式按钮.像这样:

I'm writing a small menubar application for OS X Yosemite. When clicking on the menubar icon, a popover appears with a number of recessed buttons. Like so:

如您所见,即使默认情况下选择了d20,"d4"按钮也会被聚焦.单击任何其他按钮都不会从"d4"按钮更改焦点.调焦环的唯一方法是通过Tab键.

As you can see, the "d4" button is focused, even though the d20 is selected by default. Clicking on any other button does not change the focus from the "d4" button. The only way to move the focus ring is via the Tab key.

这是我来自弹出视图控制器的代码:

Here is my code from the popover view controller:

import Cocoa

class DiceRollerViewController: NSViewController {

    var result: Int = 0

    var currentNumberOfSides = 20

    @IBOutlet weak var rollButton: NSButton!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func sidesButtonSelected(sender: NSButton?) {
        for view in self.view.subviews as [NSView] {
            if let btn = view as? NSButton {
                if btn.tag == 1 {
                    if btn != sender {
                        btn.state = 0
                    }
                    else {
                        btn.state = 1
                    }
                }
            }
        }
    }

    @IBAction func rollDice(sender: AnyObject?) {
        willChangeValueForKey("result")
        result = DiceRoller.rollDice(numberOfRolls: 1, numberOfSides: 20)
        didChangeValueForKey("result")
    }

}

以及相关的AppDelegate代码:

And the relevant AppDelegate code:

func showPopover(sender: AnyObject?) {
        if let button = statusItem.button {
            popover.showRelativeToRect(button.bounds, ofView: button, preferredEdge: NSRectEdge.MinY)
        }
    }

总而言之,我试图摆脱单击弹出框时自动显示的聚焦环.但是,出于可访问性的原因,我不想删除通过标签进行聚焦的功能.

To summarize, I'm trying to get rid of the focus ring that appears automatically when clicking on the popover. However, I don't want to remove the ability to focus via tab for accessibility reasons.

推荐答案

尝试覆盖viewWillAppear()进行滚动!"通过执行以下操作将第一响应者设置为按钮:

Try overriding viewWillAppear() to make the "Roll!" button the first responder by doing:

if theRollButton.acceptsFirstResponder {
    self.view.window.makeFirstResponder(theRollButton)
}

这篇关于如何将焦点设置为NSButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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