为什么我的 UIViewController 不在其视图的响应者链中? [英] Why isn't my UIViewController in the responder chain for its view?

查看:15
本文介绍了为什么我的 UIViewController 不在其视图的响应者链中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了 UIViewController 的一个子类,它以编程方式创建视图,而不是从 NIB 文件中加载它.

I have written a subclass of UIViewController which creates a view programmatically, instead of loading it from a NIB file.

它有一个简单的loadView方法:

- (void)loadView
{
    UIScrollView *mainScrollView =
        [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = mainScrollView;
    [mainScrollView release];
}

...然后我在 viewDidLoad 中进行大部分初始化,如文档所述.一切正常,我可以看到带有自定义视图的滚动视图.

...then I do the bulk of my initialization in viewDidLoad, as documented. It all works, and I can see the scroll view with my custom views in it.

我需要一个 UIViewController 来拥有视图,因为它是 UINavigationBar 工作流的一部分.因为我有一个控制器对象,我宁愿它做控制器的事情.

I need a UIViewController to own the view because it's part of a UINavigationBar workflow. Since I have a controller object, I'd rather it do the controller stuff.

那么,问题是我的视图控制器似乎不在响应者链中.touchesBegan:withEvent: 如果我在根视图或子视图中定义它会被调用,但如果它在视图控制器本身中则不会被调用.

The problem, then, is my view controller does not seem to be in the responder chain. touchesBegan:withEvent: is invoked if I define it in the root view or a subview, but not if it's in the view controller itself.

Apple 事件处理文档流畅地提到了 视图控制器应该在响应者链中.UIViewController 文档没有说明除了将根视图分配给 self.view 属性之外所需的额外步骤,正如我在上面所做的那样.UIResponder 文档声称 UIView 应该确定它是否有控制器并将事件传递给它.UIScrollView 文档什么也没说.

Apple event handling documentation glibly mentions the view controller should be in the responder chain. UIViewController documentation says nothing about extra steps needed beyond assigning the root view to the self.view property, as I've done above. UIResponder documentation claims a UIView should figure out if it has a controller and pass the event to it. UIScrollView documentation says nothing at all.

我还针对所有视图和子视图尝试了 userInteractionEnabled: 的各种设置,但没有成功.

I've also experimented with various settings of userInteractionEnabled: for all views and subviews, with no luck.

我错过了什么?

推荐答案

EricB,触摸只有在没有被处理的情况下才会被发送到响应者链.UIScrollView 显然处理所有的触摸事件,所以它不会向它的 nextResponder 发送任何东西.对我来说很有意义.

EricB, touches are sent down the responder chain only if they have not been handled. UIScrollView obviously handles all the touch events, so it does not send anything to it's nextResponder. Makes perfect sense to me.

你真正想要的是在 UIScrollView 的滚动逻辑处理之前过滤"触摸事件.但请注意,响应者链对于这项工作来说是一个错误的工具,正是因为它不允许在事件得到处理之前拦截它们.

What you actually want is to "filter" touch events before they are handled by the scrolling logic of UIScrollView. But note that the responder chain is a wrong tool for this job, exactly because it does not allow to intercept the events before they get handled.

在您的情况下,可能最好的解决方案是继承 UIScrollView,覆盖触摸方法(touchesBegan 等)并在调用 [super touchesXxx] 之前手动将事件发送到委托.

Probably the best solution in your case is to subclass UIScrollView, override the touch methods (touchesBegan etc) and manually send the events to the delegate before calling [super touchesXxx].

这篇关于为什么我的 UIViewController 不在其视图的响应者链中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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