iDevGames Forums
Newbie: Referencing Objects - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Newbie: Referencing Objects (/thread-2231.html)

Newbie: Referencing Objects - john42 - Oct 25, 2008 10:02 AM

Hi there!

Please read my question, I'm stuck and it doesn't really have anything to do with the iPhone, more with Objective-C in general.

I create a UIBarButtonItem and in the course of that I set a target: and an action:. Unlike any and all of the examples I found on the web and in the SDK I don't want the message sent to my current object, so I don't want target:self but another object I created elsewhere from another class.

How do I do that? I know I have an object in memory and I know its name but I don't have a pointer to it. For methods there is @selector, is there something like it for objects?

If this helps: In Actionscript I'd write _root.myObjectName.myFunctionName and be set. Is there a way to access objects like this in Objective-C?

Or am I doing it all wrong? Sad

Newbie: Referencing Objects - john42 - Oct 25, 2008 12:09 PM

Hm, anybody? I feel like a complete idiot because it probably is something very easy and stupid. Mad

Newbie: Referencing Objects - Blacktiger - Oct 25, 2008 06:38 PM

I'm having a bit of trouble understanding your situation, thus making it hard to answer your question. Are you programmatically creating your UIBarButtonItem or have you put it in a NIB file? And what do you mean by you know the name of the object? Objects don't have names, they have reference variables which have names.
What you want is to supply a reference to your object for the target.
int main()
  id myObject = [[ButtonHandlerClass alloc] init]; // myObject is a reference variable
  UIBarButtonItem*  = [[UIBarButtonItem alloc]
The key thing to note here is that myObject is only a reference, you could have multiple references to the same object with different names. Alternatively, your reference could be a member variable which you connect through interface builder, then you just use the member in whatever method creates your UIBarButtonItem.

(I got the UIBarButtonItem creation code off of Apple's forums at and just changed the target name. Not that I expect that to be a problem with the NDA fixed anyway.)

Newbie: Referencing Objects - john42 - Oct 25, 2008 09:49 PM

Thank you very much for your answer! I think in spite of my clumsy explanation you got my question right.

In Actionscript (Adobe Flash) every object has an instance name. Thus you can traverse the object hierarchy if you know the names of all objects (starting with _root). I thought I could look up an object by its instance name somehow. But I see this is not possible.

So the only way is to pass a reference for my target object to all the subview objects? That sounds kind of cumbersome. But then again all I know is scripting languages (Javascript, Actionscript, PHP, etc...). Maybe this is the "usual" way with Objective C?

Newbie: Referencing Objects - Blacktiger - Oct 26, 2008 11:18 AM

The typical way to deal with what you are describing is to create your interface in Interface Builder and connect that interface (also in IB) to custom controller objects.

Why do you want to change the target of your UITabBarItem? As far as I know, those are only supposed to change the view displayed in your tabbed interface and while you could certainly make them do other stuff you will likely just confuse your users.