Re: -viewDidMoveToWindow without subclassing? NSViewController?
Re: -viewDidMoveToWindow without subclassing? NSViewController?
- Subject: Re: -viewDidMoveToWindow without subclassing? NSViewController?
- From: Jerry Krinock <email@hidden>
- Date: Thu, 16 Apr 2009 08:43:35 -0700
On 2009 Apr 15, at 22:14, Michael Ash wrote:
IMO that's a silly objection. Subclassing and adding this method is
easy, straightforward, and it works. You only need to override one
method to justify having a subclass.
Thanks Michael. I decided to subclass the view that needs the
initialization (NSPredicateEditor) instead of the tab view item's
content view. So now it has some other code, relocated from the
window controller. But it's the same idea.
I'm still having trouble with NSPredicateEditor's -
viewDidMoveToWindow, though. But before I pursue that I'd like to
continue discussion of Graham's comment, to make sure I'm on the
beaten path...
On 2009 Apr 15, at 20:09, Graham Cox wrote:
Views that are not in the currently selected tab are not in the view
hierarchy ... [notShownView window] is always nil. It doesn't matter
when you call it - if it's not visible in the selected tab you can't
find the window that way.
Conversly, when a tab is visible, any visible view in it will return
the valid -window. Since I only need it once, during initialization,
I grab it during -viewDidMoveToWindow.
If you need the window, the window controller will return it from -
window.
The other way around, Graham. I need the window controller, so I get
it from -window. See below.
I don't see why you need to get this from an item in a tab view?
You talk about the need to initialise something. What? How? What are
you trying to do?
In general, I need the document during initialization, so I do
[[[aView window] windowController] document]
Here are two examples of why I need the document:
* In an NSPredicateEditor subclass, during -viewDidMoveToWindow,
creating the row templates requires a reference to the document's
managed object context, so it can get attribute types. See code [1].
* In an NSOutlineView subclass, during -viewDidMoveToWindow, I need
to initialize and set a data source, which needs a (weak) reference to
the document in order to get data objects. See code [2].
Is this an abnormal design pattern? I think I've seen people set
their document as nib File's Owner, but I can't do that since my
document is sometimes instantiated without a window. My nib File's
Owner is a window controller.
Jerry
[1] In implementing a subclass of NSPredicateEditor...
- (void)viewDidMoveToWindow {
// To make sure this only runs once...
if ([self isInitialized]) {
return ;
}
[self setIsInitialized:YES] ;
NSManagedObjectContext* moc = [[[[self window] windowController]
document] managedObjectContext] ;
NSEntityDescription* entityDescription = [NSEntityDescription
entityForName:@"Stark_entity"
inManagedObjectContext:moc] ;
NSArray* rowTemplates = [self
templatesWithAttributeKeyPaths:keyPaths
inEntityDescription:entityDescription] ;
[self addRowTemplates:rowTemplates] ;
...
}
[2] In implementing a subclass of NSOutlineView...
- (void)viewDidMoveToWindow {
// To make sure this only runs once...
if ([self dataSource]) {
return ;
}
// Create and set data source
Bookshelf* document_ = [[[self window] windowController]
document] ;
ContentDataSource* dataSource = [[ContentDataSource alloc]
initWithDocument:document_] ;
[self setDataSource: dataSource] ;
[dataSource release] ;
// Set the doubleclick action.
[self setTarget:[[[self window] windowController] document]] ;
[self setDoubleAction:@selector(visitItems:)] ;
...
}
_______________________________________________
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