开发者

How can I force entry to complete in a NSTextField from a NSButton

I just noticed a problem when my user-interface is in a certain state. I have a table view wi开发者_高级运维th two columns both of which the user can enter data that is to be used later. There's also a button which acts upon the contents of the table view.

The problem is if the user has entered new data in the column but has not yet exited the field by using the tab key or return key (i.e. the cursor is still in the field and in editing mode) and the button is pressed the old value is used not the current value sitting in the field.

What is the best way to handle this this? I want to use whatever the user has entered thus far.

Basically, The button code needs to tell the text field to finish completion or exit the editing mode. But I can't seem to find a method that will do that.


Use bindings. In Interface Builder, select the table column and in the Inspector go to Table Column Bindings and set the Value content binding appropriately and ensure the "Continuously Updates Values" option is checked. Then changes to the table cell content will propagate immediately.


Found the answer, At least for me.

Find out if a row is selected and if so deselect it. This causes the current entry to be completed.

- (void) completeTableEntry 
{
    // If a column is selected ensure it is completed
    NSInteger sr = [keyValueTable selectedRow];
    if (sr != -1) {
        [keyValueTable deselectRow:sr];
    }
}


How about:

[theTargetWindowWhateverThatIs endEditingFor:nil];

theTargetWindowWhateverThatIs may be, for example, self.window if you are inside a NSWindowController.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜