How to set focus to an NSButton?

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: enter image description here

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.

  • Running iOS Unit Tests from Command Line with Xcode 4.5
  • ASIFormDataRequest works on simulator but not on device?
  • Where does iPhone simulator put data files?
  • How do I run a subset of OCUnit tests in XCode
  • CocoaPods dependency only on debug/release build
  • iPhone 6 Plus resolution confusion: Xcode or Apple's website? for development
  • 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() {
        @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?) {
            result = DiceRoller.rollDice(numberOfRolls: 1, numberOfSides: 20)

    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.

    Thank you!

    Solutions Collect From Internet About “How to set focus to an NSButton?”

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

    if theRollButton.acceptsFirstResponder {