tags:

views:

275

answers:

2

There are times when it makes sense to force an end of any edits that are currently being made. For instance, if the user is typing a value in a field on my document, and then clicks to the preview window, or chooses "Print" or "Save" from the menu.

The problem is, by forcing an end to editing (either by asking the NSWindow to make itself first responder, or by calling endEditingFor: on the window), the first responder focus is no longer on the field in question. This is disruptive to the user in some situations, where the action being taken is iterative and does not signify an end to their work on the document.

My first attempt at solving this is to pay attention whatever the current firstResponder is, and then to restore it after ending editing, by using NSWindow's "makeFirstResponder:". This works OK, but it has the undesired effect e.g. on NSTextFields of resetting the selection in the field editor to the entire length of the string contents.

Is there some trick I can use to force the entire system of "endEditing" methods to be called without disrupting the current field editor at all?

Thanks, Daniel

+3  A: 

Why not use your original method but also record where/what the selection range looks like and restore it after restoring the first responder?

Joel Levin
Thanks, maybe this is the best thing to try to do. I'll see how it goes.
danielpunkass
It does do the trick, thanks. I was worried about catching edge cases correctly but for my purposes (mainly restoring selection in field editor for focused text field), what works very well is getting the selectedRanges from the field editor and setSelectedRanges: after I'm done.
danielpunkass
A: 

I agree that this is the way to do it. On iPhone it is particularly important not to have the keyboard jumping up and down at inopportune moments. I have used:

[textView endEditing:YES];
[textView becomeFirstResponder];

successfully to complete pending spelling correction (as though a space were hit) before taking action on the content of a UITextView, but without any side-effects.

Steve Weller