Reputation: 1
I want to implement simple drag and drop in my application. If you drag a file into window I want to return the file-path with NSLog. Here is my code but nothing happens if I drag a file. By the way I connected AppDelegate with referencing outlets with window (delegate) to receive everything from 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
Upvotes: 0
Views: 1191
Reputation: 186
This is an old question, but here is how I solved this problem:
I made a subclass of NSWindow
, named it MainWindow
. Then I added the protocol NSDraggingDestination
to the new class MainWindow
. In the Interface Builder, I selected the application window, and put MainWindow
as classname in the Identity Inspector.
In the AppDelegate
:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[_window registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
}
And the implementation of MainWindow
:
#import "MainWindow.h"
@implementation MainWindow
- (void)awakeFromNib {
[super awakeFromNib];
printf("Awake\n");
}
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
printf("Enter\n");
return NSDragOperationEvery;
}
/*
- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender {
return NSDragOperationEvery;
}
*/
- (void)draggingExited:(id<NSDraggingInfo>)sender {
printf("Exit\n");
}
- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender {
printf("Prepare\n");
return YES;
}
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
printf("Perform\n");
NSPasteboard *pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
unsigned long numberOfFiles = [files count];
printf("%lu\n", numberOfFiles);
}
return YES;
}
- (void)concludeDragOperation:(id<NSDraggingInfo>)sender {
printf("Conclude\n");
}
@end
Works like a charm! (Finally!)
Upvotes: 2
Reputation: 64002
Only objects which occupy screen space -- windows or views -- can accept and handle dragging events. Your app delegate is neither of those. Further, the window does not send any message it receives to its delegate. It only sends messages that are part of the NSWindowDelegate
protocol. You need to implement this dragging code in a view class, an instance of which appears on the screen.
Upvotes: 2