Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 子视图未接收到接触_Objective C_Ios_Touches - Fatal编程技术网

Objective c 子视图未接收到接触

Objective c 子视图未接收到接触,objective-c,ios,touches,Objective C,Ios,Touches,我有一个主视图控制器,它添加了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 displ

我有一个主视图控制器,它添加了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];


}
在这里,我们将子视图添加到主视图控制器。这两个子视图都是使用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视图控制器中的某些动画也可以正常工作,但这些子视图都不接收触摸事件

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

我尝试将主视图控制器userInteraction设置为NO,并没有实现touchesbeent方法,但这只会使触摸被忽略

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

我确实读到过类似的问题,但回答毫无帮助

我在子视图中有这个

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

提前感谢您的帮助。

我很确定问题在于您调用
[super touches begind:touchs with event:event]

从该方法的文档中:

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


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

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


解决方案:多亏了Simon Goldeen,我发现在一个演示项目中,TouchesStart被调用。然而,在我的生产项目中,它不是。事情是这样的:(正如苹果推荐的)你和我正在使用
-(void)release以减少内存使用。这导致touches开始不被调用。Simon不使用
release
,因此在他的情况下,会调用它们。

两个子视图都启用了用户交互吗?是的,它们都有用户交互。在您的touchsbegind方法中,您会怎么做?您是否在它们的开头放了一个简单的NSLog来验证它们是否被调用?我已经编辑了我的问题,以在子视图中显示touchesBegind方法。UIViewController是UIResponder的子类,如果它的视图没有响应该方法,它将接收-
touchesBegind:
方法调用。它没有。这是有道理的,因为你不能触摸视图控制器,你可以触摸视图控制器拥有(控制)的视图…我理解你的反应。我现在将对代码进行一些更改。谢谢你们两位的快速回复。brbI刚刚编写了一个快速测试应用程序来验证UIViewController是否收到了
-touchesBegind:
方法调用。没有办法:|#正在自己编写一个测试应用程序。。。编辑:很抱歉,确实是这样(这根本没有意义,因为文档的某些部分甚至说它不起作用)…删除那一行不起作用,我实际上使用了没有那一行的原始方法,但添加了它,希望它有助于删除那一行会起到相反的作用。由于super检查应该调用哪些子视图,因此现在将不再调用某些子视图。您还应该注意,iOS从根到细节都在工作,这意味着它将从您的UIWindow开始,最终在包含特定触摸位置的最深嵌套UIView中结束。