Newbie: Referencing Objects
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?
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?
Hm, anybody? I feel like a complete idiot because it probably is something very easy and stupid.
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.
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 http://discussions.apple.com/thread.jspa...5&tstart=0 and just changed the target name. Not that I expect that to be a problem with the NDA fixed anyway.)
What you want is to supply a reference to your object for the target.
Code:
int main()
{
id myObject = [[ButtonHandlerClass alloc] init]; // myObject is a reference variable
UIBarButtonItem* = [[UIBarButtonItem alloc]
initWithTitle:@"Button1"
style:UIBarButtonItemStyleBordered
target:myObject
action:@selector(button1Pressed:)];
}(I got the UIBarButtonItem creation code off of Apple's forums at http://discussions.apple.com/thread.jspa...5&tstart=0 and just changed the target name. Not that I expect that to be a problem with the NDA fixed anyway.)
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?
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?
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.
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Getting around Python's referencing of variables | Nevada | 2 | 2,876 |
Mar 10, 2007 01:07 PM Last Post: Nevada |
|

