NSTableView triggering another action when clicked
While learning objective-c, I've made a basic mac shopping list app with 3 interface elements: NSTextField
, NSButton
and NSTableView
. The basic idea is you type something into the text field, hit the add button (or hit return) and it adds it to the table view. Everything works as expected until I click on the table view at which point it adds what ever is currently to the text field to the开发者_如何学JAVA table view.
The only action I have is addItem
which adds the text field's stringValue
to NSTableView
's data source, an NSMutableArray
. I've set it to be only triggered by the button and the textfield, not the NSTableView
. When I remove the action from NSTextField
the problem stops. Any suggestions?
The NSTextField
fires its action both when the user hits the Enter key and when it loses focus. When you click on the NSTableView
, the NSTextField
loses focus, so it calls its action.
If you only want it to send its action on Enter, there is a pop up button in the NSTextField
's attributes tab labeled "Action" that you can set to "Sent on Enter Only".
精彩评论