views:

34

answers:

2

I have inherited a new class from NSWindowController to implement windowDidLoad and then access to the NIB defined window controls:

- ( void ) windowDidLoad
{
   NSArray * controls = [ [ [ self window ] contentView ] subviews ];
   int i;

   NSRunAlertPanel( @"windowDidLoad", @"", @"OK", NULL, NULL );

   if( [ controls count ] == 0 )
      NSRunAlertPanel( @"no hay controles", @"", @"OK", NULL, NULL );   

   for( i = 0; i < [ controls count ]; i++ )
      NSRunAlertPanel( @"control", @"", @"OK", NULL, NULL );   
}

code execution goes fine. The NIB window is shown, but subviews has no elements. How to access the window child controls ? Thanks,

A: 

It seems as window is nil. How to access it from windowDidLoad ?? thanks,

AL FT
This is a separate question; it does not answer your question, so you should not post it here as an answer. You should post it as a separate question.
Peter Hosey
A: 

The NIB window is shown, but subviews has no elements.

You probably forgot to hook up the window outlet to your window. When the outlet is not hooked up, the outlet property holds nil, so [self window] returns nil.

Then you send messages to nil. I say “messages” because every message to nil does nothing and returns nil, 0, or 0.0 as appropriate. That means you send the contentView message to nil, so that returns nil, which means you send the subviews message to nil, so that also returns nil.

As I said, a message to nil returns nil, 0, or 0.0; when you send the count message to controls, since controls is nil as explained in the previous paragraph, that message returns 0.

The fix is to open your nib in IB and connect your controller's window outlet to your window.

By the way, you should not use indexes to loop over NSArrays. There is a simpler, cleaner way to do it: Fast Enumeration.

Peter Hosey
The inherited class windowController is created programmatically. How to hook up the window outlet ? thanks,
AL FT
I think you're confusing a class with an instance. The window controller object is an instance of the NSWindowController class, or of a class that inherits from NSWindowController. Doing something “programmatically” refers to doing it in one or more statements; creating a class programmatically is very difficult (not to mention pointless), so you almost certainly mean instantiating it, which is irrelevant: There is no other way to instantiate an NSWindowController. (You *could* create one in a nib, but there would be no point.)
Peter Hosey
As for connecting the outlet: http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/IB_UserGuide/
Peter Hosey
I did not explain it properly. I instantiate the windowController object programmatically. The question is how do I hook up the NIB window to my controller so data window is not nil ? thanks,
AL FT
Peter, pointing to the entire docs is not very useful :)
AL FT
"The fix is to open your nib in IB and connect your controller's window outlet to your window" The controller does not exist in IB so I can not set its window outlet. Thats what I am looking for, thanks
AL FT
This post seems to point in a better direction:http://osdir.com/ml/cocoa-dev/2010-07/msg00252.html"Did you remember to hook up the window in the nib to File's Owner'swindow outlet? Did you remember to change the class identity of File's Owner to your NSWindowController subclass?"
AL FT
Working with the above solution! Problem solved :-)
AL FT
AL FT
That's not “a better direction”, that's the direction recommended by the docs.
Peter Hosey