Re: Enumerating outlets automatically?
Re: Enumerating outlets automatically?
- Subject: Re: Enumerating outlets automatically?
- From: "Michael Ash" <email@hidden>
- Date: Thu, 6 Nov 2008 12:50:55 -0500
On Thu, Nov 6, 2008 at 12:15 PM, Andy Lee <email@hidden> wrote:
> On Nov 6, 2008, at 11:27 AM, Michael Ash wrote:
>>
>> 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 ...
>>>
>>>
> [...]
>>
>> 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.
>
> I'm not sure I understand -- would the proxy object have outlets to
> myOutlet1, myOutlet2, etc.?
It wouldn't have outlets in the sense of IBOutlet ivars declared in
the header. You would, however, "lie" to IB and tell it that it did
have all the outlets you want to manage, then connect them. You would
then implement forwarding like so (not tested, buyer beware, etc.):
- (void)forwardInvocation:(NSInvocation *)inv {
NSString *selStr = NSStringFromSelector([inv selector]);
if([selStr hasPrefix:@"set"] && [[inv methodSignature]
numberOfArgument] == 3) {
NSString *key = [[selStr substringWithRange:NSMakeRange(3, 1)]
lowercaseString];
key = [key stringByAppendingString:[selStr substringFromIndex:4]];
id argument = nil;
[inv getArgument:&argument atIndex:2];
[outletDictionary setObject:argument forKey:key];
[realOutletObject setValue:argument forKey:key];
}
else
[super forwardInvocation:inv];
}
You'd also want an override of -respondsToSelector: to convince the
nib loader that you really do implement these methods, and, of course,
you'd need an implementation of -methodSignatureForSelector: for the
forwarding to work.
This would be a lot easier if the outlet connecting machinery simply
used KVC, but to my knowledge it does not. If I'm wrong then of course
you can replace that whole big thing with a pretty simple override of
-setValue:forKey:.
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