当窗口包含单个 WebView 视图时接收 NSWindow 上的 Drag 事件
我对 Objective-C 和 Cocoa 很陌生,所以请为我分解一下。我正在开发一个由另一位开发人员启动的项目,并且只在 Objective-C 中工作了 3 天。
我有一个包含 WebView 视图的 NSWindow 子类。加载的WebView内容是一个Silverlight插件。我注册了 NSWindow 来接收 Drag 事件。仅当拖动发生在 NSWindow 标题栏中时才会生成拖动事件。我在加载方法中注册了拖动事件。
AdminWindow.mm
#import "AdminWindow.h"
#import "NativeMessageReceiver.h"
extern AdminWindow* adminRiaWindow;
@implementation AdminWindow
@synthesize adminWebView;
BOOL isAdminContentLoaded;
-(void) load
{
if (!isAdminContentLoaded)
{
NSLog(@"loading Admin window");
NSString *curDir = [[NSBundle mainBundle] bundlePath];
NSString* url = [NSString stringWithFormat: @"file://%@/Contents/Resources/RIA/AdminContentMac.html",curDir];
[[adminWebView mainFrame] loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: url]]];
[adminWebView setDrawsBackground:NO];
id win = [adminWebView windowScriptObject];
NativeMessageReceiver* receiver = [NativeMessageReceiver getInstance];
[win setValue:receiver forKey:@"NativeMessageReceiver"];
receiver.adminWebView = adminWebView;
isAdminContentLoaded = YES;
}
}
-(void) show
{
[self load];
[self setIsVisible: YES];
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
[self makeKeyAndOrderFront: self];
[self makeMainWindow];
[self center];
}
-(void) hide
{
[self setIsVisible: NO];
}
- ( BOOL ) windowShouldClose : ( id ) sender
{
[self setIsVisible: NO];
return NO;
}
- (BOOL) canBecomeKeyWindow
{
return YES;
}
- (BOOL) canBecomeMainWindow
{
return YES;
}
@end
extern "C" void ShowAdminWindow()
{
NSLog(@"showing Admin window");
if (![NSThread isMainThread])
[adminRiaWindow performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
else
{
[adminRiaWindow show];
}
}
extern "C" void HideAdminWindow()
{
if (![NSThread isMainThread])
{
[adminRiaWindow performSelectorOnMainThread:@selector(hide) withObject:nil waitUntilDone:YES];
}
else
{
[adminRiaWindow hide];
}
}
CustomeWebView
@implementation SteamPunkWebView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
NSLog(@"In Custom View Init");
}
return self;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您只在标题栏中收到拖动事件,因为您注册了窗口来接收事件,而不是网络视图。
试试这个:
I think you're only getting drag events in the title bar because you registered the window to receive the events, not the web view.
Try this instead:
WebView
是 Cocoa 中最复杂的视图之一,在拖动方面是一种特殊情况。您不需要实现标准的NSView
拖动行为,而是需要将一个对象设置为 Web 视图的WebUIDelegate
并实现以下委托方法:-webView:dragDestinationActionMaskForDraggingInfo:
然后,您可以控制 Web 视图如何响应拖动操作。
WebUIDelegate
协议还有其他几种控制拖动的方法,因此您应该阅读 文档了解更多详细信息。要实际接收拖动的对象并处理它们,您需要子类化
WebView
并实现-draggingEnded:
。您绝对不需要向窗口本身添加任何拖动方法。
WebView
s are one of the most complex views in Cocoa and are a special case when it comes to dragging. Instead of implementing the standardNSView
dragging behaviour, you need instead to set an object as the web view'sWebUIDelegate
and implement the following delegate method:‑webView:dragDestinationActionMaskForDraggingInfo:
You can then control how you want the web view to respond to the drag action. The
WebUIDelegate
protocol has several other methods to control dragging, so you should read the documentation for more detail.To actually receive the dragged objects and process them, you will need to subclass
WebView
and implement‑draggingEnded:
.You definitely do not need to add any dragging methods to the window itself.