转发拖拽& drop事件到父视图 [英] Forwarding drag & drop event to parent view

查看:184
本文介绍了转发拖拽& drop事件到父视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中我有一个自定义视图从NSView派生。
在这个视图中,有几个自定义子视图,也是派生自NSView。

I have an application where I have one custom view which is derived from NSView. Within this view, there are several custom subviews, which are also derived from NSView.

我想实现一个拖放行为,允许URL下降到视图。一切都已经为主视图工作。

I want to implement a drag and drop behavior which allows URLs to be dropped onto the views. Everything is already working for the main view.

因此,实际上我需要在子视图和父视图类上实现拖拽行为处理程序。
事情是,我不想将完整的处理代码复制到所有的子视图,使它们也接受拖动事件。所以我认为这将是最好的方法,只是让他们转发所有拖动事件到父视图。

So, actually I would have to implement dragging behavior handlers on the child-views and the parent-view class. The thing is, that I don't want to copy the complete handling code to all the child-views to make them also accept drag events. So I thought that it would be the best way to just let them forward all drag events to the parent view.

这是可能的吗?不确定我是否可以用响应链可以设置这个?

Is this possible somehow?? Not sure if I can somehow set this up with the responder-chain maybe?

任何提示都非常感谢!
提前感谢。

Any tips are highly appreciated!! Thanks in advance.

推荐答案

我遇到了类似的问题,

I faced a similar issue where I wanted anything dropped in a view, or any of it's subviews to be processed by the view, but the calls never got there.

经过一些研究,我发现这个答案是最有帮助的: http://stackoverflow.com/a/7389711/327471

After some research I found this answer to be the most helpful: http://stackoverflow.com/a/7389711/327471

基本上如果视图未注册接收任何拖动事件,它应该将该信息自动传递到其父视图。所以在我的情况下,我最终得到这样的东西:

Essentially if a view is not registered to receive any drag events it should pass that information up to it's parent view automatically. So in my case I ended up with something like this:

NSArray *subviews = [self.view subviews];
for (NSView *aSubview in subviews) {
    [aSubview unregisterDraggedTypes];
}

当然你可以比这更精确,某种类型的子类或任何你想要的参数。但最终关键是从它的拖动类型取消注册问题子视图。

Of course you can be more precise than that, and make sure to only check subclasses of a certain type or whatever parameters you want. But ultimately the key was unregistering the problem subview from it's dragged types.

我希望这有助于。

这篇关于转发拖拽& drop事件到父视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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