views:

56

answers:

1

Hi,

Any idea why after replacing one UIView with another (with the same Touch event logic), the 2nd one won't pick up any touch events?

I'm replacing them using:

[currentView removeFromSuperview]; NewView *newView = [[NewView alloc] init]; [window addSubview:newView];

Thank you all for your help :)

A: 

Is your NewView class set up to receive touch events in the first place? ("User Interaction Enabled" checkbox set in Interface Builder, or, userInteractionEnabled BOOL property set to YES in UIView)?

Is a UIView below NewView in your view hierarchy (i.e. a child of NewView) consuming touch events in a touchesBegan:withEvent: method?

Shaggy Frog
The old view that disappears to the left stops tracing touch events, but the new one doesn't pick them up either.My initial XIB points to Step1 (1st view) and then thru the code above, I swap the view with Step2, so there's no way of setting userInteractionEnabled in Interface Builder and doing it in the actual View changes nothing :(I think it must relate to the View's owner, but im a total noob here, so not sure.Is there a way to tell the view controller, initially initiated with the XIB, to point to the new View's class?thanks for your help
Andre
You say you have a xib but you also say "there's no way of setting userInteractionEnabled in Interface Builder". This is not true. If you have a xib for NewView (my guess is it's named NewView.xib) you can double-click on it to open it up in IB, and then see that checkbox in the View Attributes section of the Attributes Inspector.
Shaggy Frog
You're right, I meant there's no way of doing it, for a Class not associated with it.So the initial XIB is associated with Step1 in the IB. Then I remove the Step1 view and add Step2.
Andre
Just figured out that changing the "init" in my above code to: initWithFrame:CGRectMake(0, 0, 480, 320)]; makes some buttons work.any thoughts why?cheers
Andre
Oh geez, I didn't notice that before. You do need to use initWithFrame if you're creating the view in code. What do you mean by "some buttons" work?
Shaggy Frog
I have 5 UIImageViews, defined like this:image = [UIImage imageNamed:@"button.png"];bt1 = [ [ UIImageView alloc ] initWithFrame:CGRectMake(14, 261, image.size.width, image.size.height) ];bt1.image = image; bt1.userInteractionEnabled = YES;[self addSubview:bt1];all of the 5 have userInteractionEnabled = YES, but only BT1 actually works.very odd :/
Andre
So if I test the following under "touchesBegan":UITouch *touch = [touches anyObject];NSLog(@"touched on", [touch view]);It just traces out "touched on", instead of "touched on <UIImageView: 0x3d20cf0; (etc etc etc)" like it does on the Step1 view.
Andre
My fault :D the NSLog wasn't properly formatted. Still checking what could be causing the actual event fail
Andre
Solved it!the initWithFrame:CGRectMake(0, 0, 480, 320)] i had copied, was for a landscape app, so the frame was actually not covering some of my buttons. changed it to 320,480 and it works great now.Thanks a bunch for your help ;)
Andre