子 uiview 的 UIViewcontroller 未检测到触摸

发布于 2024-11-07 16:09:16 字数 1032 浏览 0 评论 0原文

大家好,我有一个 UIViewController、RootUIViewController 引用另一个 UIViewcontroller、MainMenuViewController。

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

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

}

Hey guys i have a a UIViewController, RootUIViewController referencing another UIViewcontroller, MainMenuViewController.

Im adding MainMenuViewController's view as a subview to RootUIViewController's view. The problem is touch events are not being caught in MainMenuViewController touchesBegan method.

Relevant code is below. The output when touching the screen show "touched at root view controller". The desired result i want is the touch event to be caught in the MainMenuViewController and display "touched at root view controller". What am I missing/ doing wrong here?

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");

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我还不会笑 2024-11-14 16:09:16

很高兴您能够解决内存管理问题。我想添加一个警告,指出

[self.view addSubview:m_mainMenuViewController.view];

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

我试图在这里详细介绍这一点:滥用 UIViewControllers,希望这能有所帮助。

Glad you were able to resolve your memory management issue. I want to add a warning that

[self.view addSubview:m_mainMenuViewController.view];

is problematic and, in my opinion, a bad idea. Subviews of a UIViewController's view should not be managed by their own UIViewController. Those subviews can have controllers but they should not be UIViewController subclasses because they will never reliably behave exactly like you might expect from a UIViewController and that is likely to cause a headache for you later. Better to accept the limits of the classes and API we get from Apple and design a supported, reliable solution.

I've tried to cover this in detail here: Abusing UIViewControllers, hopefully that's some help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文