tags:

views:

165

answers:

6

When a Cocoa NIB file instantiates an instance of a custom controller object, what is the name of the variable that that custom controller instance is assigned to?

In case that isn't clear, if you manually created an instance of that class you would do:

MyControllerClass *myVar = [[MyControllerClass alloc] init];

What equivalent of "myVar" has the NIB used when doing this behind the scenes?

+3  A: 

There is no such thing as a variable name once the app is compiled, so this question doesn't make much sense. In your example, myVar is just a convenient label for you, the programmer, and does not exist in any way once your source code is compiled into binary code.

When you place an object into a nib file, it is archived and then unarchived at runtime. If you want to be able to get a reference to an object that has been archived in a nib file, you need to use an outlet, which means you declare an IBOutlet instance variable in a class that is present in the nib file and then connect that outlet to the object in the nib you want to reference in Interface Builder. Instance variables are different to the stack variable that you declared in your example and can be referred to at runtime.

Typically you would have an object that "owns" a nib. Normally nibs are loaded by an instance of NSWindowController or NSViewController and window or view controller is represented in the nib file as File's Owner. If you declare outlets in your window/view controller, you can then connect the outlets from File's Owner to your object in Interface Builder.

So, to clarify, you need a reference to your object in the nib from some other object in the same nib. That second object declares an outlet using the IBOutlet keyword on an instance variable like so:

@interface SomeOtherObject : NSObject
{
    IBOutlet SomeObject* anObject;
}
@end

In Interface Builder, you can then connect the anObject outlet of the SomeOtherObject instance to the first SomeObject instance. You can do this by control-dragging from one object to another or you can do it in the connections panel in the Interface Builder inspector.

You can then refer to your SomeObject instance by the variable name anObject inside the code for SomeOtherObject.

Rob Keniger
A: 

@ Rob Keninger (I can't post comments):

Thank you. In general, "convenient label for you, the programmer" is what I'm after.

I'm not talking about the objects "in" the NIB file (button, etc.), but about a controller class that you have created and linked to the NIB by putting a custom "Object" in the NIB.

Like, you push a button in the NIB, and that runs the method "buttonGotPushed" in "MyCustomControllerClass" that you have coded in Xcode. When the app starts up, an instance of the MyCustomControllerClass class has been created by the NIB (or by something in the framework).

Something has to point to that instance in memory, hopefully a "convenient label for me the programmer" : )

My question is, what is the name of that convenient label/variable pointing to it in memory -- how can I communicate with THAT specific instance of the class (created by the NIB), rather than any other arbitrarily-created instances of the same class.

Nibbles
Like for example, if I manually create the instance ' MyControllerClass *myVar = [[MyControllerClass alloc] init]; ' -- then any object in the app can invoke [myVar someInstanceMethod]. I can code for that because I know what "myVar" is called. But when the NIB has created the instance of MyControllerClass, I currently am faced with [mysteryVarName someInstanceMethod]. See what I'm after?
Nibbles
Like I mentioned, you need to declare an outlet in another object and connect it to your object. I've added some clarification to my answer, hopefully that makes it clearer. It sounds like you need to do a basic Cocoa tutorial. The Apple Currency Converter tutorial is a great start: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjCTutorial/
Rob Keniger
Nibbles: The Stack Overflow FAQ says you can comment on answers on your own questions, even with only 1 reputation. http://stackoverflow.com/faq
Peter Hosey
+2  A: 

Implement the awakeFromNib method in your controller class - it's called immediately after the nib has finished loading, and your controller's instance can be found in the "self" variable.

tedge
That's a good option. Certainly easier to explain...
Rob Keniger
A: 

@ tedge (I can't make comments to your answer):

Can you clarify for a beginning Cocoa learner a bit. Take the Apple Currency Converter tutorial.

I implement the awakeFromNib method in the existing ConverterController class. (Something I will be learning to do shortly!)

The app starts up and an instance of ConverterController is automatically instantiated.

What will awakeFromNib tell me about that running instance (other than that it's ready to use) -- and what syntax with "self" gets it to divulge that information?

Nibbles
A: 

… what is the name of the variable that that custom controller instance is assigned to?

It's whatever name you gave that variable when you declared it.

IB doesn't create variables for you. It sounds like you're after an outlet, which is a variable you create that IB knows about and lets you plug objects into, thereby setting the variable.

(You actually can create outlets from IB, and in the modern runtime, this should really create the outlet, not just declare a non-existent outlet in the nib. Even this way, though, you create the outlet [in IB] and you give it a name.)

Peter Hosey
A: 

I think what's Nibbles is confused about is that how to reference the variable defined only in NIB file from code.

the answer to that is, normally you have a custom controller class (or delegate class) A in code and NIB, and if you have another class or controller B only defined in NIB, just setup a outlet in A pointing to B. Since A can be used anywhere in your code, B can be accessed as well through A then.

I had this question as well.

windmaomao