subviews are not receiving touches

谁说我不能喝 提交于 2019-12-12 19:29:47

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5873167/subviews-are-not-receiving-touches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!