开发者

Cocoa: what is the var name of an instance created by a NIB file?

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开发者_如何学Go used when doing this behind the scenes?


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.


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 (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?


… 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.)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜