Xcode set default button on enter when making a fo

2019-03-27 14:41发布

I'm making a form in Xcode using objective-c and cocoa. In the interface builder I have 2 textfields and 2 buttons. When I build the project and run it, neither of the two buttons are selected (highlighted in blue as opposed to white). When the user is writing in the text field the ENTER key needs to trigger one of the buttons to be pressed.

How do I do this?

2条回答
疯言疯语
2楼-- · 2019-03-27 15:22

A link to Apple's documentation is here: http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/Button/Articles/MakingaButtontheDefaultButton.html

this code sets the return key to be the default:

[myButton setKeyEquivalent:@"\r"];

not mentioned in the docs, but to programmatically remove that key, you would use:

[myButton setKeyEquivalent:@""];
查看更多
放我归山
3楼-- · 2019-03-27 15:45

You set the Key Equivalent value for your button in IB. Just click on that field in the attributes inspector and press the enter key.

查看更多
登录 后发表回答