How to stop my NSbutton from choosing when the application starts by default?

I created a button and I have a little problem. When the application starts, the button is selected. How to disable this choice?

Example:

A window with two buttons. The first (left) has a blue glow around it, and the second (right) does not.

+4
source share
3 answers

Set the button focus ring type to none:

[myButton setFocusRingType:NSFocusRingTypeNone]; 

You can also set this parameter in XIB.

+8
source

Firstly, you should know that by default buttons cannot get focus. The user would have to select System Preferences> Keyboard> Shortcuts> Full Keyboard Access: all controls. If they did, they may want the button to be pressed initially.

In any case, the correct way to do this is to call [theWindow makeFirstResponder:nil] some time after showing it for the first time. When it is needed, it depends on how the window is shown. If you explicitly specify it in the code, you can make a call right after that. If this is shown because its Visible at Launch flag is set in its NIB, you can do this after loading the NIB. Etc.

+2
source

Something should always be the first responder in the window, if something could be. As a rule, only the first controls, such as text fields, can become the first responder, but when the user has access to Full Keyboard Access, it is normal to select the default button.

If you do not want this particular button to start, set the initialFirstResponder window to another control.

I would suggest using -[NSWindow makeFirstResponder:nil] . The window will start with nothing selected, but the button will be selected as soon as the user types a tab. This is unusual for Mac applications because there is no way to return the window to the “nothing is selected” state as a user.

0
source

All Articles