2011-05-16 29 views
1

嘿家伙我有一个UIViewController,RootUIViewController引用另一个UIViewcontroller,MainMenuViewController。sub uiview的UIViewcontroller没有检测到触摸

我将MainMenuViewController视图作为子视图添加到RootUIViewController视图。问题是触摸事件没有被MainMenuViewController touchesBegan方法捕获。

相关代码如下。触摸屏幕时的输出显示“触摸到根视图控制器”。我想要的结果是要在MainMenuViewController中捕获的触摸事件,并显示“在根视图控制器处触摸”。我在这里错过/做错了什么?

RootUIViewController.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    MainMenuViewController* mainMenuViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuView" bundle:nil]; 

    m_mainMenuViewController = mainMenuViewController; 
    [self.view addSubview:m_mainMenuViewController.view]; 
    [mainMenuViewController release]; 

} 

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
     NSLog(@"touched at root view controller"); 

    } 

MainMenuViewController.m

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"touched at main view controller"); 

} 
+0

你到底在做什么? – rptwsthi 2011-05-16 12:50:30

+0

这里的代码看起来不错。检查'm_mainMenuViewController.view'的'userInteractionEnabled'是否设置为'NO'。 – 2011-05-16 13:02:11

+0

@rptwsthi我的目标是当我触摸视图(m_mainMenuViewController.view)时,事件将被捕获在m_mainMenuViewController touchesBegan方法中。 @Deepak谢谢生病了 – valmo 2011-05-16 13:08:42

回答

2

高兴你能够解决您的内存管理的问题。我想补充一个警告,

[self.view addSubview:m_mainMenuViewController.view];

是有问题的,在我看来,一个坏主意。 UIViewController视图的子视图不应该由他们自己的UIViewController管理。这些子视图可以有控制器,但它们不应该是UIViewController的子类,因为它们永远不会像UIViewController所期望的那样可靠地运行,这可能会在稍后导致头痛。更好地接受我们从Apple获得的类和API的限制,并设计一个受支持的,可靠的解决方案。

我试过在这里详细讲述这个问题:Abusing UIViewControllers,希望这有些帮助。