子视图没有收到触摸 [英] subviews are not receiving touches

查看:123
本文介绍了子视图没有收到触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

两个子视图都能正确显示,即使MenuSceneBackground视图控制器中的某些动画也可以正常工作,但是这些子视图均未收到触摸事件.

Both subviews display correctly, even some animations in the MenuSceneBackground view controller work fine, but none of these subviews receive touch events.

它们都实现了touchesBegan,但只有主View Controller接收到它们.

They all implement touchesBegan but only the main View Controller receives them.

我尝试将主View Controller userInteraction设置为NO,并且未实现touchesBegan方法,但这只会使触摸被忽略.

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.

我在子视图中有这个

- (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事件. (您在视图控制器中而不是在视图控制器中添加了touchesBegan ...).

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

解决方案:感谢Simon Goldeen,我发现在一个演示项目中,touchesBegan被调用.但是,在我的生产项目中却没有.事情是这样的:(按照Apple的建议)您和我正在使用- (void)release;来减少内存使用量. 这将导致touchesBegan不被调用. Simon不使用release,因此在他的情况下,确实会调用它们.

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.

这篇关于子视图没有收到触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆