2

我有添加 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];


}

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

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

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

4

2 回答 2

2

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

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

于 2011-05-03T17:41:45.837 回答
0

我很确定问题出在你打电话的时候[super touchesBegan:touches withEvent:event];

从该方法的UIResponder文档中:

此方法的默认实现什么也不做。然而,UIResponder 的直接 UIKit 子类,尤其是 UIView,将消息转发到响应者链。

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

于 2011-05-03T17:40:31.427 回答