On Sun, 16 Oct 2011 13:53:05 -0700, Eeyore <eey...@monsterworks.com> said: >I noticed that I many of my IBOutlets were only being used to modify the view >from the viewDidLoad methods but which were not accessed later in my code. >These outlets exist so that I can keep consistent appearance settings in a >large number of nibs without actually editing each nib. As a simplified >example (typed into Mail, but should describe the pattern), consider the >following. > >----- begin new style ----- >@interface Class : UIViewController >@property (nonatomic, assign) IBOutlet UILabel* label; >@end > >@implementation Class >- (void)setLabel:(UILabel*)label >{ > label.textColor = [UIColor lightGray]; > label.text = NSLocalizedString(@"Blah blah blah", @"Label text"); > >} > >- (UILabel*)label >{ > NSAssert(NO, @"label is inaccessible"); > return nil; >} >@end >----- end new style -----
Very ingenious, but doesn't it leave a lot of stuff lying around that is just an accident waiting to happen? You are assuming that setLabel: will be called only by the nib-loading mechanism; but how do you know? Someone else can call setLabel: later and get very weird results. Also, you're making some assumptions (it seems to me) about when things happen, and those assumptions could sometimes be wrong. What's the problem that needs solving, really? I'd say there are two: (1) There's a lot of buttressing just to support one little ivar that you're only going to refer to once, to initialize it. One might argue that this hardly matters, since the buttressing is created for you automatically either when you drag to create the outlet or by ARC, but let's pass on that for a moment. (2) You have a desire to initialize these objects in "the right place", where that isn't the nib. But surely, then, there are better solutions. How about a self-initializing label (a UILabel subclass)? How about creating the label itself in code, not in the nib at all? How about giving the label, if created in the nib, a tag number (how much trouble can that be, since you had to go into the nib in order to make the label in the first place) so that no ivar is needed in order to find it? I could probably go on and on... m. -- matt neuburg, phd = m...@tidbits.com, <http://www.apeth.net/matt/> A fool + a tool + an autorelease pool = cool! Programming iOS 4! http://www.apeth.net/matt/default.html#iosbook_______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com