I am coding a single window, multiview, app, and as what I think is a recommended design pattern I only load the views controllers (and therefore their views) from their nibs as they are required, and then I retain them in the main window controller so they do not have to be loaded again each time the user switches from one view to another that has been previously shown.

As part of my application, I also need the views to react to changes on several conditions of the whole application, an thus for each view I set up observers to the relevant keyPaths of the application delegate by using:

[[NSApp delegate] addObserver:self forKeyPath:@"myKey" options:NSKeyValueObservingOptionNew context:nil] ;

 in the view controller awakeFromNib method and

[[NSApp delegate] removeObserver:self forKeyPath:@"myKey"] ;

in the view controller dealloc method

So far so good.

However, the way I am doing it makes me wonder where the best place should be for adding and removing the observers. What I am currently doing is adding the observers in the awakeFromNib methods of each viewController, and removing them in the dealloc method of the same controller. I think that adding the observers in awakeFromNib methods is fine, but the fact that I retain the view controllers implies that such controllers are still observing the application delegate even if they are not currently showing. Is this a good coding/design pattern?.

Furthermore, if I eventually switch to Garbage Collection in the future, those observers will not ever be removed. Or worse, in case I decided not to keep a reference in my window controller, and the GC eventually claims an unused view controller, then what will happen to the observers.

Would someone bring a light to this.? Basically what i am asking is, where is the recommended place to add and to remove observers set up in views (or view controllers)?

Thanks for any comment.

Joan Lluch _______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to