tags:

views:

225

answers:

3

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?

+6  A: 

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.

Ben Gottlieb
The problem is that I need to create multiple instances of MyObject but send different messages to different instances.
Christopher Farnell
You can create multiple outlets, and multiple objects within IB.
Ben Gottlieb
Thanks- so the instance is nameless, right? Why does no one want to admit that?- I was on the verge of using memory addresses!
Christopher Farnell
Don't use single leading underscores in your ivar names. That's an internal Apple coding convention.
NSResponder
'nameless' doesn't really mean anything here. NOTHING is 'named' in objective-C. There's a member variable for each object. That's as close to a name as you're gonna get.
Ben Gottlieb
Why such a semantic stonewall-I create an instance in code and I can send it a message-I create it in IB and I can't, not directly-its still an instance of the same class!
Christopher Farnell
A: 

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.

Peter Hosey
A: 

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.

Preston