I've reworked my model, view and controller classes in the way you've indicated 
below. Thanks.

I do have another question about this kind of setup. In the process I decided 
that it would be a good idea to have a designated initializer for my custom 
view that sets a reference to the model object using a weak property like this: 
'initWithModel: (MyModel *) model'.
This seems to work fine, and I now have a reference to my model object from 
MyDocument, MyWindowController and also from MyView.

Next I wanted to have MyView observe some model properties, so I set the view 
object as an observer from within the 'initWithModel:' initializer, like this : 
'self.model addObserver: self keyName: @"property" ....

My question now is , where I should remove MyView as an observer with 
'self.model removeObserver: self forKey: @"property"...' '?
I tried to do this in 'dealloc', but it seems that from time to time my model 
object is pulled right from under my when I close the document window, 
resulting in an error stating that MyModel was deallocated while it still had 
observers attached to it. Strangely, this doen't every time though, but just 
from time to time.

For now, I add and remove the custom view as an observer from 
MyWindowController, which seems to work fine, but I'm still not sure why I 
can't do this in the MyView object itself.

-- 
Luc Van Bogaert

On 10 Aug 2014, at 23:05, Quincey Morris <quinceymor...@rivergatesoftware.com> 
wrote:

> On Aug 10, 2014, at 13:16 , Luc Van Bogaert <luc.van.boga...@me.com> wrote:
> 
>> Let's see if I understand this correctly: do you mean I could create a 
>> separate model class, eg. "Drawing" with all of it's properties and 
>> reference this in my document class as an instance variable or even as a 
>> property.
>> Then, from my custom view, instead of using [[self.window.windowController 
>> document] someProperty]', I could use '[self.window.windowController 
>> someProperty]', which in turn would message my model object to return the 
>> value of 'someProperty' using 'return [self.document someProperty]' ? 
> 
> To be specific:
> 
> In a document-based app, I will generally have MyDocument, MyWinController 
> and MyModel.
> 
> MyDocument has a MyModel property, referencing the model that it loaded from 
> the document file (or whatever).
> 
> MyWinController has a MyModel property, referencing the same model as the 
> document.
> 
> A custom MyView would also have a MyModel property referencing the same 
> model, initialized in any of various ways depending on how the view was 
> created. The view would then use myModel.someProperty whenever it needed it, 
> perhaps even binding to it.
> 
> Putting a derived “someProperty” directly on the window controller, or even 
> on a view controller, *is* feasible too. Often, I find, I use this approach 
> when the view presents a somewhat transformed picture of the data model. 
> Databasey kinds of apps (such as master/detail) often seem to want to use the 
> real data model directly. UIey kinds of apps often seem to want to use a 
> transformed model.
> 

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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

This email sent to arch...@mail-archive.com

Reply via email to