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
.