Posts Tagged code reuse

UIView Manipulation Made Easier with a Category

I was watching a presentation recently where the presenter showed the header for a category that he had added to UIView to make his life a little easier. The point of the talk did not center around the category so I never saw much more than the header, but that was all I needed to recreate it for my use. Here’s the header:

#import <uikit/UIKit.h>
 
@interface UIView (MFAdditions)
 
- (id) initWithParent:(UIView *)parent;
+ (id) viewWithParent:(UIView *)parent;
 
// Position of the top-left corner in superview's coordinates
@property CGPoint position;
@property CGFloat x;
@property CGFloat y;
 
// Setting size keeps the position (top-left corner) constant
@property CGSize size;
@property CGFloat width;
@property CGFloat height;
 
@end

As you can see there isn’t a whole lot to this category, but if you’re doing a lot of view manipulation the benefits of it will rapidly become clear. There was one sticky wicket that I hit when implementing this class and it centers around this method:

+ (id) viewWithParent:(UIView *)parent;

When I first implemented this method I wrote it like so:

+ (id) viewWithParent:(UIView *)parent {
	return [[[UIView alloc] initWithParent:parent] autorelease];
}

This was all well and good as long as the only class that I was creating was a UIView, but I ran into trouble when I started creating UIImageViews. Instantiating new UIImageViews worked fine, but as soon as I called a UIImageView-specific method the app would crash:

*** -[UIView setImage:]: unrecognized selector sent to instance 0xd1b350

I struggled with the answer to this one for a while and it wasn’t until I presented my problem to the local CocoaHeads group did I get it all figured out. Here’s the correct way to write this method:

+ (id) viewWithParent:(UIView *)parent {
	return [[[self alloc] initWithParent:parent] autorelease];
}

By calling self instead of strongly typing the returned object as a UIView the class would dynamically determine the correct type at runtime.

You can download this category here: UIViewAdditions

, , , , , , ,

8 Comments

Typinator and Xcode

I recently acquired a free license of Typinator through MacHeist and have been loving it.  I put together a set of abbreviations for the text macros that  Xcode provides and it has made my coding life much easier.  A few folks have asked why I’m not using xctxtmacros and the truth is that I didn’t know about them.  Either way, Typinator is working great.

You can download my set of Xcode Abbreviations here.  Please note that it’s extremely limited as I only included those macros that I need most often.  Leave a comment if there are any that you think I’m missing.

,

1 Comment

Mac Developer Roundtable 16: Code Reuse

macdeveloperroundtableI had the good fortune of recording another episode of the Mac Developer Roundtable a few weeks ago and that episode is now live on the Mac Developer Network site.

We have a rousing discussion on code reuse and all its pluses and minuses,  I highly recommend giving it a listen.

This episode is free, but the Mac Developer Network also provides a large amount of fantastic premium content including video training courses, free software, software discounts, plus access to other podcasts that are not available to non-members.  Head on over to MDN’s Why Join? page to find out more.

For those of you who aren’t subscribed to the MDR feed through iTunes, here is a direct link to episode 16: Mac Developer Network » Blog Archive » MDR016: Code Reuse.

, , ,

No Comments