开发者

initWithCoder breaking my touch events (touchBegan, touchMoved, etc)

开发者 https://www.devze.com 2022-12-31 13:38 出处:网络
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.

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 bl开发者_StackOverflowank)

- (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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消