15 Most Recent [RSS]
More...
|
What is an Outlet?
I'm just reading Steve Weller's blog about his quest to learn Cocoa, and I remembered how hard it was for me to understand what an IBOutlet and a Connection (like Target/Action) in Interface Builder are. So, I thought in case anyone has the same prior knowledge I had when learning Cocoa, and the same mindset, it might be helpful if I just offered the description of these two as I have them in my head:
Outlet: Is simply a pointer to an object. You add it to your class, and control-drag from the object containing the pointer to the object being pointed to. This is handy, because instead of having to do a call to GetControlByID() to look up an object every time, you can just access it directly, using a pointer that IB will set up for you when loading. This is also why there is the File's Owner: There has to be one already existing object that will receive all those pointers to the newly-created objects, (which you can get by adding the needed outlets to File's Owner).
Target/Action: When I started out, I thought this was something different from an outlet, but actually it's simply a special case. target is an outlet, and action is a method name (or rather, it's a SEL, if that means anything to you at this point). So, what this does is give the NSControl view in question not just an object to send a message to. No, it also gives it the name of the message to send to this object. That way, you can have one controller react to several button clicks with different actions, without needing to switch on the sender parameter.
I hope this helps someone out there. It probably won't be of much use to programming newbies, but people who are already seasoned with C or C++ might benefit.
| |