Saving CoreData context and binding problem
I use CoreData for a Mac application which runs under 10.5 and higher. The content of an entity (text attribute) will be displayed in an NSTextView through bindings. The user can edit the text there.
When I try to save the managed object context, the mouse cursor disappears from the text view and the text view scrolls up to the top.
How can I prevent C开发者_JAVA百科oreData/binding to do this because it's annoying for the user to lose the focus!
Edit: Added the solution as a separate answer to mark the question as completed!
Take a look at commitEditing before save. This causes the textview to lose the focus or even revert all changes in the current textfield. I don't think you can prevent this behavior. You could restore the selection and cursor position after saving. I also would be interested in an actual solution to this problem.
I just found a solution for this problem. Before saving the managed object context I save the "state" (selected range and scroll position) of the text view
NSRange selectedRange = [self.textView selectedRange];
NSScrollView* scrollView = [self.textView enclosingScrollView];
// get the current scroll position of the document view
NSPoint scrollPosition = [[scrollView contentView] bounds].origin;
after saving my entities I restore the selected range and scroll position
[self.textView setSelectedRange:selectedRange];
// restore the scroll location
[[scrollView contentView] scrollToPoint: scrollPosition];
[scrollView reflectScrolledClipView: [scrollView contentView]];
I found 2 possible solution to set the scroll position of a scroll view
- Apple documentation about NSScrollView which does not work for me
- Quickies about Scrolling an NSScrollView programatically which works!
精彩评论