subviews are not receiving touches
I have the main View controller which adds 2 subviews.
- (void)viewDidLoad
{
[self init];
[super viewDidLoad];
//To shrink the view to fit below the status bar.
self.wantsFullScreenLayout = YES;
//We start off by displaying the background Images.
[self displayMenuSceneBackground];
//Then we show the Menus.
[self displayMenuSceneMenu];
}
Here we add the subviews to the main View Controller. both subviews are view controllers built using interface builder.
-(void) displayMenuSceneBackground{
//Display the MenuSceneBackground View Controller
MenuSceneBackground *screen = [[MenuSceneBackground alloc] init];
[self.view addSubview:screen.view];
[screen release];
}
-(void) displayMenuSceneMenu{
//Display the MenuSceneMenu View Controller
MenuSceneMenu *screen = [[MenuSceneMenu alloc] init];
[self.view addSubview:screen.view];
[screen release];
}
Both subviews display correctly, even some animations in the MenuSceneBackgro开发者_JAVA技巧und view controller work fine, but none of these subviews receive touch events.
They all implement touchesBegan but only the main View Controller receives them.
I tried making the main View Controller userInteraction to NO, and not implementing the touchesBegan method but this only makes the touches to be ignored.
Both subviews display on the whole screen size.
I did read similar problems but the responses were of no help.
I have this in the sub views
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSLog(@"testing MenuSceneMenu");
[self clicked_testing_alert]; //This is a UIAlertView for debugging
return;
}
Thanks for the help in advance.
Please note that an UIViewController isn't the same as an UIView and thus isn't receiving touchesBegan-events. (you added touchesBegan in your view controller instead of in your view...).
Solution: thanks to Simon Goldeen, I found out that in a demo-project, touchesBegan gets called. However, in my production-projects, it doesn't. Here's the thing: (as Apple recommends) you and I are using - (void)release;
to decrease memory usage. This causes touchesBegan not to be called. Simon doesn't use release
, so in his situation, they do get called.
I am pretty sure the problem is when you call [super touchesBegan:touches withEvent:event];
From the UIResponder documentation for that method:
The default implementation of this method does nothing. However immediate UIKit subclasses of UIResponder, particularly UIView, forward the message up the responder chain.
So, the default behavior of UIViewController is to pass the touch up the responder chain. Since this isn't something you want to do (at least not immediately) you should either remove that line from your code, or include it after you have determined that you don't need or want to respond to the touch in your current view.
精彩评论