Re: -viewDidMoveToWindow without subclassing? NSViewController?

2009-04-17 Thread John C. Randolph
On a general note, if you want something to happen later than - 
awakeFromNib, you can always send - 
performSelector:withObject:afterDelay:cancelPrevious:.  If you pass a  
delay of zero, it will happen the next time through the event loop.   
I've used this many times to deal with situations similar to what  
Jerry described.


-jcr


"This is not a book to be tossed aside lightly. Rather, it should be  
hurled with great force." -Dorothy Parker


___

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 arch...@mail-archive.com


Re: -viewDidMoveToWindow without subclassing? NSViewController?

2009-04-16 Thread Graham Cox


On 17/04/2009, at 1:43 AM, Jerry Krinock wrote:

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.



This does sound like an abnormal design pattern. The controller should  
be in charge, so going from view -> window -> controller -> document  
seems backwards. The controller's initialization (whether at  
awakeFromNib or windowDidLoad time) should be handling this, so you  
can do self -> document to access the document. The controller is  
responsible for forging the necessary links and set up between any  
views it knows about and the document. The view definitely shouldn't  
be doing this, the controller definitely should be.


I've not used NSPredicateEditor, so I'm not familiar with it, but as a  
view it shouldn't be talking directly to the data model. Instead, the  
controller should give it what it needs as appropriate.


The same pattern definitely applies to NSOutlineView, which I am  
familiar with. The controller is typically its datasource, which you  
can set in IB. The controller then gathers data from the "real"  
datasource (the document, say) and presents it to the view.  
Effectively the controller proxies the data from the document on  
demand. This approach requires no special initialisation - the  
NSOutlineView won't ask for data until it's ready to display it, at  
which point it must by definition be visible and in a window. The  
controller can then just fetch (and possibly cache) data from the  
document and return it to the outline view in the usual way. I'd be  
surprised if the same technique would be inappropriate for the  
predicate editor also.


Trying to hook into certain moments like when a view is moved to the  
window sounds error prone, tricky, and unnecessary. I've never found  
the need to do that sort of thing using window controllers - let the  
views come to the window controller for their data, and let the window  
controller be in charge of obtaining it. It seems like you're trying  
to short-circuit the controller and simply using it to make a direct  
connection between the document and the view - this is probably where  
you're going wrong. Place the controller in the data pathway, don't  
just use it to make a pathway directly between data model and view.


--Graham


___

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 arch...@mail-archive.com


Re: -viewDidMoveToWindow without subclassing? NSViewController?

2009-04-16 Thread Jerry Krinock


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 (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 arch...@mail-archive.com


Re: -viewDidMoveToWindow without subclassing? NSViewController?

2009-04-15 Thread Michael Ash
On Wed, Apr 15, 2009 at 10:09 PM, Jerry Krinock  wrote:
> I have the contentView of an NSTabViewItem controlled by an
> NSViewController.  I need to run code in there to initialize things when the
> nib is loaded, but not before the view has a -window.
>
> Putting the code in -awakeFromNib is no good because it will not yet be in a
> window if another tab is selected during nib loading.  I believe it would
> work to subclass the content view and implement -viewDidMoveToWindow, but
> that seems silly since it would be the only code in this subclass.

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.

Mike
___

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 arch...@mail-archive.com


Re: -viewDidMoveToWindow without subclassing? NSViewController?

2009-04-15 Thread Graham Cox


On 16/04/2009, at 12:59 PM, Jerry Krinock wrote:



On 2009 Apr 15, at 19:15, Graham Cox wrote:

The usual way is to implement -windowDidLoad (a NSWindowController  
method) and put your code in there. This is called when the window  
is first shown by the window controller, since windows are  
constructed lazily.


Thanks Graham, but after trying this I discover that it suffers from  
the same problem as -awakeFromNib.  Indeed it runs later than - 
awakeFromNib, but still not late enough because [self window] still  
returns nil in views that are not in the initially-selected tab view  
item.




Isn't this always going to be the case with NSTabView?

Views that are not in the currently selected tab are not in the view  
hierarchy - the tab's "container" view is retained by the tab but not  
added to the tab view itself unless selected, so there is no path from  
any hidden view item to the window - [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.


If you need the window, the window controller will return it from - 
window. 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?


--Graham




___

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 arch...@mail-archive.com


Re: -viewDidMoveToWindow without subclassing? NSViewController?

2009-04-15 Thread Jerry Krinock


On 2009 Apr 15, at 19:15, Graham Cox wrote:

The usual way is to implement -windowDidLoad (a NSWindowController  
method) and put your code in there. This is called when the window  
is first shown by the window controller, since windows are  
constructed lazily.


Thanks Graham, but after trying this I discover that it suffers from  
the same problem as -awakeFromNib.  Indeed it runs later than - 
awakeFromNib, but still not late enough because [self window] still  
returns nil in views that are not in the initially-selected tab view  
item.


___

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 arch...@mail-archive.com


Re: -viewDidMoveToWindow without subclassing? NSViewController?

2009-04-15 Thread Graham Cox


On 16/04/2009, at 12:09 PM, Jerry Krinock wrote:

I have the contentView of an NSTabViewItem controlled by an  
NSViewController.  I need to run code in there to initialize things  
when the nib is loaded, but not before the view has a -window.


Putting the code in -awakeFromNib is no good because it will not yet  
be in a window if another tab is selected during nib loading.  I  
believe it would work to subclass the content view and implement - 
viewDidMoveToWindow, but that seems silly since it would be the only  
code in this subclass.


It seems like NSViewController should give me a "view has moved to  
window" notification or delegate message, but I can't find any.   
There is a -loadView, but Cocoa does not invoke it when loading or  
moving the view.


Is there a better way -- or, what have I done wrong?



The usual way is to implement -windowDidLoad (a NSWindowController  
method) and put your code in there. This is called when the window is  
first shown by the window controller, since windows are constructed  
lazily.


--Graham


___

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 arch...@mail-archive.com


-viewDidMoveToWindow without subclassing? NSViewController?

2009-04-15 Thread Jerry Krinock
I have the contentView of an NSTabViewItem controlled by an  
NSViewController.  I need to run code in there to initialize things  
when the nib is loaded, but not before the view has a -window.


Putting the code in -awakeFromNib is no good because it will not yet  
be in a window if another tab is selected during nib loading.  I  
believe it would work to subclass the content view and implement - 
viewDidMoveToWindow, but that seems silly since it would be the only  
code in this subclass.


It seems like NSViewController should give me a "view has moved to  
window" notification or delegate message, but I can't find any.  There  
is a -loadView, but Cocoa does not invoke it when loading or moving  
the view.


Is there a better way -- or, what have I done wrong?

Jerry

___

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 arch...@mail-archive.com