Search code examples
objective-ciostouches

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


Solution

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