English 中文(简体)
Cocoa: what is the var name of an instance created by a NIB file?
原标题:
  • 时间:2010-02-04 07:22:24
  •  标签:
  • cocoa
  • nib
  • xib

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?

问题回答

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.





相关问题
Taking picture with QTCaptureView

Is it possible to simply take a picture and save it somewhere using a QTCaptureView and Apple s built-in iSight? I ve seen lots of tutorials on recording video but none on simply taking a picture. Any ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

How to get a flash url in a webpage using a webframe?

As we know , When we load frame from webpage of safari, we will invoke the delegate methods of webkit informal protocol(WebFrameLoadDelegate): webView:didStartProvisionalLoadForFrame: webView:...

热门标签