Faire une classe appelée DragDropView qui sous-classe NSView et définir la vue en MainMenu.xib être de ce type (Sélectionnez votre point de vue, allez à l'identité Inspecor et écrire dans DragDropView classe personnalisée).
Écrivez le code (voir ci-dessous) pour DragDropView et exécutez-le. Une fenêtre vide devrait apparaître.
Faites glisser des dossiers sur votre fenêtre. Vous devriez obtenir les chemins des dossiers écrits dans votre console. Quelque chose comme.
2014-02-01 11: 18: 10,435 Démarrer [41767: 303] ( "/ Users/bob/Desktop/Heathers Animations", "/Users/bob/Desktop/bird.atlas" )
// DragDropView.h
#import <Cocoa/Cocoa.h>
@interface DragDropView : NSView
@end
// DragDropView.m
#import "DragDropView.h"
@implementation DragDropView {
BOOL isHighlighted;
}
- (void)awakeFromNib {
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
}
- (BOOL)isHighlighted {
return isHighlighted;
}
- (void)setHighlighted:(BOOL)value {
isHighlighted = value;
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)frame {
[super drawRect:frame];
if (isHighlighted) {
[NSBezierPath setDefaultLineWidth:6.0];
[[NSColor keyboardFocusIndicatorColor] set];
[NSBezierPath strokeRect:frame];
}
}
#pragma mark - Dragging
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
NSPasteboard *pboard = [sender draggingPasteboard];
if ([[pboard types] containsObject:NSFilenamesPboardType]) {
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
for (NSString *path in paths) {
NSError *error = nil;
NSString *utiType = [[NSWorkspace sharedWorkspace]
typeOfFile:path error:&error];
if (![[NSWorkspace sharedWorkspace]
type:utiType conformsToType:(id)kUTTypeFolder]) {
[self setHighlighted:NO];
return NSDragOperationNone;
}
}
}
[self setHighlighted:YES];
return NSDragOperationEvery;
}
- (void)draggingExited:(id <NSDraggingInfo>)sender {
[self setHighlighted:NO];
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
return YES;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
[self setHighlighted:NO];
return YES;
}
- (void)concludeDragOperation:(id<NSDraggingInfo>)sender {
NSArray *files = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];
NSLog(@"%@", files);
}
@end
Merci, je vais chercher ça. – nosedive25
Les questions vagues obtiennent des réponses vagues; C'est bien que vous ayez le temps d'écrire une classe de démonstration pour l'affiche originale, mais pas tout le monde. Tenter de faire honte à quelqu'un près de quatre ans après le fait, cependant, est sans doute le moins utile de toute action ... –
Wrong, c'est une chaîne: p Vous pouvez l'utiliser avec '[NSArray arrayWithObject: NSFilenamesPboardType]' –