我想在我的应用程序中实现简单的拖放。如果将文件拖到窗口中,我想用 NSLog 返回文件路径。这是我的代码,但如果我拖动文件,什么也不会发生。顺便说一句,我将 AppDelegate 与带有窗口(委托)的引用插座连接起来,以接收来自窗口的所有内容。
AppDelegate.m:
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[_window registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
}
-(NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender
{
return NSDragOperationGeneric;
}
-(BOOL)prepareForDragOperation:(id < NSDraggingInfo >)sender
{
NSPasteboard* pbrd = [sender draggingPasteboard];
NSArray *draggedFilePaths = [pbrd propertyListForType:NSFilenamesPboardType];
// Do something here.
return YES;
NSLog(@"string2 is %@",draggedFilePaths);}
@end
AppDelegate.h:
//
// AppDelegate.h
// testdrag
//
// Created by admin on 18.07.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@end