In XCode, when you create an instance of a class in IB rather than in the code, what is the name of the instance object?

If I create a new instance of a class with IB I can't send messages to it because I don't know its name. I have asked several people and they always say 'you just use files owner' but that doesn't answer my question. What is the instances name?

Upvotes: 0

Views: 1556

Answers (3)

user155959
user155959

Reputation:

It sounds like you're new, so you should run through Apple's Currency Converter tutorial and make sure you're up to speed on IBOutlets and how they work. They're the pointers you use to access the objects in a nib file.

Upvotes: 0

Peter Hosey
Peter Hosey

Reputation: 96333

Objects don't have names (NS/UIImages excepted, but that's different). In order to refer to an object in a nib by name, you must have an instance variable with that name, make that instance variable an outlet (with the IBOutlet keyword), and connect the outlet to the object in IB.

Upvotes: 0

Ben Gottlieb
Ben Gottlieb

Reputation: 85532

In order to send a message to an object you created in IB, you have to hook it up to something's outlet. For example, if you create an instance of MyObject, you'll be able to reference it by creating an out let in your file's owner that points to MyObject, and hooking it up.

//Header file
@interface MyViewController : UIViewController {
    MyObject        *_myObjectOutlet;
}
@property (nonatomic, readwrite, retain) IBOutlet MyObject *myObjectOutlet;
@end


//Implementation file
@implementation MyViewController
@synthesize myObjectOutlet = _myObjectOutlet;
...

Then, in your IB file, create an instance of MyObject, set the file owner to be an instance of MyViewController, and hook that object's myObjectOutlet up to your MyObject.

Upvotes: 6

Related Questions