views:

81

answers:

1

So I have a UIView that has been setup, and in each touch event handler I have an NSLog fire off a message to the console.

- (void) touchesBegan:(NSSSet*)touches withEvent:(UIEvent*)event {
    NSLog(@"touchesBegan");
}

And that pretty much works as expected. But once I implement initWithCoder (even blank)

- (id)initWithCoder:(NSCoder*)coder {
   return self;
}

I no longer receive the message to my console (or can hit breakpoints obviously).

This is my first app so I'm probably missing something dumb, but I've looked through various example apps and I don't appear to be missing any code that would re-enable touch events.

+4  A: 

You need to treat -initWithCoder: as you would init, even for a blank implentation; that is to say:

- (id) initWithCoder:(NSCoder *)coder {
  self = [super initWithCoder:coder];
  if (self) {
    // Do initializations. Or not.
  }
  return self;
}

Only one init message is typically called for each object, unless you chain them. As such, if -initWithCoder: is called, -init isn't. And when -init isn't called, oh boy…

As way of clarification: Objects loaded from nibs are initialized with -initWithCoder:, which in the default implementation obviously calls -init somewhere.

Williham Totland