Ignore touches to UIView subclass, but not to its subviews

Note: I’ve moved all future technical/programming posts out of this blog and over to objectivesea.tumblr.com. Follow that one if you’re going to follow me for technical junk.

If you want a UIView subclass to ignore touch events, just set its userInteractionEnabled property to NO. But that will block touches to all its subviews. To ignore touches in a UIView subclass, but not its subviews, just work a little hitTest magic:

-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    id hitView = [super hitTest:point withEvent:event];
    if (hitView == self) return nil;
    else return hitView;
}

How to set default values for NSUserDefaults

NSUserDefaults is a great place to store your apps settings. It is convenient, available everywhere, and integrates nicely with settings bundles. One problem you run across with NSUserDefaults, is setting default values for your settings. There is a very simple elegant solution to this, detailed below.

Define keys

Use defines to keep keys consistent I usually have one header file just for keys I use throughout the project.

Create a defaults property list

Set default values in a new property list Create a new property list, and enter default values for all your settings. Make sure the keys match the ones you #define-d. Also pay attention to the types.

Set defaults on launch

The following code needs to run every time your app launches (in case your NSUserDefaults have gotten nuked or mangled). I added the following three lines to application:didFinishLaunchingWithOptions: in my app delegate.

NSString *defPath = [[NSBundle mainBundle] pathForResource:@"DEF" ofType:@"plist"];

NSDictionary *defaultsDict = [NSDictionary dictionaryWithContentsOfFile:defPath];

[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDict];

Fantastic, you are done.