Iphone 子uiview';UIViewcontroller未检测到触摸

Iphone 子uiview';UIViewcontroller未检测到触摸,iphone,uiview,uiviewcontroller,subview,Iphone,Uiview,Uiviewcontroller,Subview,嘿,伙计们,我有一个UIViewController,RootUIViewController引用另一个UIViewController,MainMenuViewController 我正在将MainMenuViewController的视图作为子视图添加到RootUIViewController的视图中。问题是MainMenuViewController TouchesBegind方法中未捕获触摸事件 相关代码如下。触摸屏幕时的输出显示“在根视图控制器处触摸”。我想要的结果是在MainMenu

嘿,伙计们,我有一个UIViewController,RootUIViewController引用另一个UIViewController,MainMenuViewController

我正在将MainMenuViewController的视图作为子视图添加到RootUIViewController的视图中。问题是MainMenuViewController TouchesBegind方法中未捕获触摸事件

相关代码如下。触摸屏幕时的输出显示“在根视图控制器处触摸”。我想要的结果是在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");

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

}
MainMenuViewController.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");

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

}

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

[self.view addSubview:m_mainMenuViewController.view]

在我看来,这是个有问题的坏主意。UIViewController视图的子视图不应由其自己的UIViewController管理。这些子视图可以有控制器,但它们不应该是UIViewController子类,因为它们的行为永远不会像您从UIViewController中期望的那样可靠,这可能会让您以后感到头疼。最好接受我们从Apple获得的类和API的限制,设计一个受支持的、可靠的解决方案


我已经在这里详细介绍了这一点:,希望能有所帮助。

您到底想做什么?这里的代码看起来不错。检查
m_mainMenuViewController.view的
userInteractionEnabled
是否设置为
NO
@rptwsthi我的目标是当我触摸视图(m_mainMenuViewController.view)时,事件将在m_mainMenuViewController TouchesBegined方法中捕获@迪帕克谢谢我会试试的