Re: How to detect when an NSTextView has finished being loaded into a window?
Re: How to detect when an NSTextView has finished being loaded into a window?
- Subject: Re: How to detect when an NSTextView has finished being loaded into a window?
- From: Keith Blount <email@hidden>
- Date: Tue, 22 Feb 2011 08:55:50 -0800 (PST)
Many thanks for the reply. Yes, you're right - the text view could get deallocated in -viewDidMoveToWindow which is indeed a recipe for disaster and was causing the issues. (I should have realised that in the first place, but by the time the page layout set-up got moved to a method responding to a notification sent from -viewDidMoveToWindow, I was at the end of my tether trying to put it in the right place to avoid the artefact bug, and as the program got away with not crashing for most users, I never noticed the potential problem with this move.)
The way the page view gets swapped for the text view and then the original text view gets removed entirely is actually based on code straight from TextEdit, so I think it's more the placement of the code than the way it's done.
I seem to have fixed the issue, although my solution is a bit of an ugly mash-up. In my controller I now retain the original text view in the method responding to the notification it receives from -viewDidMoveToWindow and then release it again after a delay of 0, i.e. at the end of the run loop. That way I ensure it's kept around long enough so that it doesn't get deallocated while it's still calling methods on itself (e.g. from _setWindow:). I'm also now sending the notification from the end of _setWindow: if that method is available and it's not the App Store version, otherwise after a delay of 0 from -viewDidMoveToWindow. The latter still has the issue of the slight delay where the user can see the original text view appear on screen for an instant before it gets swapped out for the page view, but that's better than a crash. Taken together, these ensure that the page view only gets loaded when there is a window for it to get loaded into, and that the
old text view is only deallocated after it's been finished with.
Part of the issue is the complex arrangement of views in my app. TextEdit can call -setHasMultiplePages: in -windowDidLoad, because the page view will move directly to the window; in my app the page view may be hidden away in a tab view at this point as there may be a different sort of view selected for viewing on project open - showing an image, or a PDF file, for instance - meaning that if I call -setHasMultiplePages: in -windowDidLoad, the page view may not have a window and so the artefacts bug will rear its head. The next obvious place for switching to multiple pages view was -tabView:didSelectTabViewItem:, so that it only got called when the text was displayed in the tab view. However, that's no good either because my app has hide-able split panes, so there's no guarantee that the tab view is on screen and part of a window when everything gets set up... This is why I ended up only switching to the multiple page view when the program knows that the
basic text view has made it to the window - that's the only time I'm guaranteed a window for the page view.
Hmm, although I'm now wondering if maybe I should just set the scroll view's documentView to nil when it's off-window and do all this when the scroll view moves to the window...
Thanks again and all the best,
Keith
--- On Tue, 2/22/11, Kyle Sluder <email@hidden> wrote:
> From: Kyle Sluder <email@hidden>
> Subject: Re: How to detect when an NSTextView has finished being loaded into a window?
> To: "Keith Blount" <email@hidden>
> Cc: "Matt Neuburg" <email@hidden>, email@hidden
> Date: Tuesday, February 22, 2011, 3:19 AM
> On Mon, Feb 21, 2011 at 1:04 PM,
> Keith Blount <email@hidden>
> wrote:
> > Hi Matt,
> >
> > Many thanks for the reply. Unfortunately that doesn't
> really work, as if it is hidden in any way then it is off
> screen and therefore the artefacts bug rears its ugly head.
> (The whole thing is in a tab view, and even if the text view
> is in a different tab to the visible one while it is set up,
> the artefacts bug appears.)
>
> Are you running on Snow Leopard?
>
> Also, from your original description, it sounds like you
> intend for
> the text view to possibly be deallocated during
> -viewDidMoveToWindow.
> This sounds like a recipe for disaster. Instead, you should
> try
> hooking up additional text views/containers to the same
> layout
> manager. Your first text view will therefore always exist;
> in
> single-view mode it will be the only text view, whereas in
> page layout
> mode it will be the first in the chain of text views
> sharing the
> layout manager and text storage.
>
> --Kyle Sluder
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden