アプリケーションに単純なドラッグ アンド ドロップを実装したいと考えています。ファイルをウィンドウにドラッグすると、NSLog でファイルパスを返したいと思います。これが私のコードですが、ファイルをドラッグしても何も起こりません。ちなみに、私はAppDelegateをwindow(delegate)でアウトレットを参照して接続し、windowからすべてを受け取るようにしました。
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