Question

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 CoreData/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!

Was it helpful?

Solution 2

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top