子视图没有接收到触摸

我有主视图控制器,它添加了2个子视图。

- (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]; } 

在这里,我们将子视图添加到主View Controller。 两个子视图都是使用界面构建器构建的视图控制器

 -(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]; } 

两个子视图都能正确显示,即使MenuSceneBackground视图控制器中的某些动画也能正常工作,但这些子视图都不会接收到触摸事件。

它们都实现了touchesBegan,但只有主视图控制器接收它们。

我尝试将主视图控制器userInteraction设置为NO,而不是实现touchesBegan方法,但这只会使触摸被忽略。

两个子视图都显示在整个屏幕大小上。

我确实读过类似的问题,但回答没有帮助。

我在子视图中有这个

 - (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; } 

我在这里先向您的帮助表示感谢。

请注意,UIViewController与UIView不同,因此不接收touchesBegan-events。 (您在视图控制器中添加了touchesBegan而不是在视图中…)。

解决方案 :感谢Simon Goldeen,我发现在一个演示项目中,touchesBegan被调用。 但是,在我的生产项目中,它没有。 这是事情:(正如Apple建议的那样)你和我正在使用- (void)release; 减少内存使用量。 这导致touchesBegan不被调用。 西蒙不使用release ,所以在他的情况下,他们会被召唤。

我很确定问题是当你打电话给[super touchesBegan:touches withEvent:event];

从该方法的UIResponder文档:

此方法的默认实现不执行任何操作。 但是,UIResponder的UIKit子类,特别是UIView,会立即将消息转发到响应者链。

因此,UIViewController的默认行为是通过触摸响应器链。 由于这不是您想要做的事情(至少不是立即),您应该从代码中删除该行,或者在确定您不需要或想要在当前视图中响应触摸后包含该行。