禁用 WebView 的拖放以支持其超级视图之一

sie*_*ied 6 macos cocoa drag-and-drop objective-c rubymotion

我正在开发一个像 Mac Mail 这样的应用程序。我有一个允许用户编写新消息的 WebView。我实现了拖放功能,以便用户可以通过这种方式向消息添加附件。

为简单起见,我有一个包含 WebView 和其他视图的主视图。我在这个主视图上实现了拖放(使用 draggingEntered: 和 performDragOperation: 方法)并且它按预期工作。

问题是默认情况下,当在 WebView 中拖动文件时,例如图像,图像将显示在 WebView 中。但我不希望那样,我希望将其添加为附件,这就是为什么我禁用了 WebView 中的拖放功能:

  def webView(sender, dragDestinationActionMaskForDraggingInfo:draggingInfo)
    WebDragDestinationActionNone
  end
Run Code Online (Sandbox Code Playgroud)

但是现在我的文件将被添加为附件,如果我将它拖动到主视图内的任何位置,除了在 WebView 中(在这种情况下不调用 draggingEntered: 和 performDragOperation: 方法)。

我不知道我的问题是否足够清楚以找到答案,我还是 Cocoa 开发的新手,所以如果您需要更多详细信息,请随时告诉我。另一件事,我正在使用 Rubymotion,但如果您在 Objective-C 中有解决方案,那也将是完美的!

感谢您的任何帮助或建议。

解决方案

我子类化了 WebView 并覆盖了该performDragOperation方法以使其工作:

def performDragOperation(sender)
  self.UIDelegate.mainView.performDragOperation(sender)
end
Run Code Online (Sandbox Code Playgroud)

Jam*_*ren 1

您可以检查目标窗口sender(实现协议):NSDraggingInfo

http://developer.apple.com/library/Mac/#documentation/Cocoa/Reference/ApplicationKit/Protocols/NSDraggingInfo_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSDraggingInfo/draggingDestinationWindow

def performDragOperation(sender)
  if sender.draggingDestinationWindow == @web_view
    # Attach file
  else
    # Let it do the normal drag operation
  end
  true
end
Run Code Online (Sandbox Code Playgroud)

这只是一个猜测,但您应该能够从这里找到解决方案。