Cocoa: Avoiding 'Updates Continuously' in control binds

别等时光非礼了梦想. 提交于 2019-12-06 11:28:07

You're right that you don't need to use the continuously updates value option.

If you're using bindings (which you are), then what you should be doing is calling the -commitEditing method of the NSController subclass that's managing the binding. You'd normally do this in your method that closes the sheet that you're displaying.

-commitEditing tells the controller to finish editing in the active control and commit the current edits to the bound object.

It's a good idea to call this whenever you are performing a persistence operation such as a save.

The solution to this is to 'end editing' in the action method that gets called by the OK button. As the pane is a subclass of NSWindowController, the NSWindow is easily accessible, however in your code you might have to get the NSWindow via a control you have bound to the controller; for example NSWindow *window = [_someControl window].

Below is the implementation of my okPressed action method.

In summary I believe this is a better solution to setting Updated Continuously in the bound controls.

- (IBAction)okPressed:(id)sender
{
    NSWindow *window = [self window];
    BOOL editingEnded = [window makeFirstResponder:window];
    if (!editingEnded)
    {
        logwrn(@"Unable to end editing");
        return;
    }

    if (_delegateRespondsToEditComplete)
    {
        [_delegate detailsEditComplete:&_mydetails];
    }
}

Although this is really old, I absolutely disagree with the assumption that this question is based on.

Countinously updating the binding is absolutely not expensive. I guess you might think this updates the value continuously, understanding as "regularly based on some interval".

But this is not true. This just means it updates whenever you change the bound value. This means, when you type something in a textView, it would update as you write; this is what you'd want in this situation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!