Re: Enumerating outlets automatically?
Re: Enumerating outlets automatically?
- Subject: Re: Enumerating outlets automatically?
- From: "Michael Ash" <email@hidden>
- Date: Thu, 6 Nov 2008 11:27:08 -0500
On Thu, Nov 6, 2008 at 12:13 AM, Graham Cox <email@hidden> wrote:
> Is there a way to enumerate the outlets of an object automatically?
>
> In a certain type of controller object I'm finding I'm writing a lot of code
> like this:
>
> [myOutlet1 setEnabled:NO];
> [myOutlet2 setEnabled:NO];
> ... etc for another several dozen outlets ...
>
> If I could get a list of outlets (maybe as a dictionary with the ivar name
> as key) I could probably find much easier ways to do stuff to each one. I
> suspect the answer is no but I thought I'd ask anyway...
There are a few ways you could do something like this.
First, the dumb easy way, simply define a method which returns an
array of outlets:
- (NSArray *)_myOutlets { return [NSArray arrayWithObjects:myOutlet1,
myOutlet2,...]; }
The downside is that you have to manually synchronize it if you add or
remove outlets. The advantage is that it takes thirty seconds to
implement and the mechanism is very obvious.
Second, you can use the ObjC runtime to enumerate all of your instance
variables, and filter out any that aren't object pointers. The trouble
with this approach is that IBOutlet only exists at compile time (it's
a macro defined to an empty string) so you can't distinguish IBOutlets
from regular object pointer ivars. This may or may not be a problem
for you.
Lastly, you could write a proxy object, put it in your nib, and hook
up all of the outlets to it instead. The proxy would intercept all
setFoo: messages by forwarding them to your real object and
simultaneously building a dictionary. This will be sure to only get
real outlets.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden