为什么我的UIViewController不在响应链中来查看它? [英] Why isn't my UIViewController in the responder chain for its view?

查看:95
本文介绍了为什么我的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];
}

...然后我在<$ c $中进行大部分初始化c> 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.

可能是你的最佳解决方案case是子类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天全站免登陆