0

I’m trying to implement а simple mechanic: after draging а file, pass it’s name to lable. But I couldn’t understand how to do this. Here is my code: H file:

 #import <Cocoa/Cocoa.h>
 @interface DropView : NSView <NSDraggingDestination> {}
 @property (assign) IBOutlet NSTextField *labelUrl; // <- label for file name
 @property NSString *urlStr;
 @end

M file:

 #import "DropView.h"
 @implementation DropView
 - (id)initWithFrame:(NSRect)frame {
      self = [super initWithFrame:frame];
     if (self) {
         [self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]];
      }
     return self;
 }
 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
     if ([sender draggingSourceOperationMask] & NSDragOperationCopy) {
         return NSDragOperationLink;
     }
     return NSDragOperationNone;
 }
 - (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender {
     return YES; 
 }
 - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
     NSPasteboard *pboard = [sender draggingPasteboard];
     if ( [[pboard types] containsObject:NSURLPboardType] )
     {
         NSURL *fileURL = [NSURL URLFromPasteboard:pboard];
         _urlStr = [[fileURL absoluteString] lastPathComponent]; // <- example "img.jpg"
         [self.labelUrl setStringValue:_urlStr]; // <- this is not work
     }
     return YES;
 }
 -(void)awakeFromNib {
     [_labelUrl setStringValue:@"no file name"]; 
 }
 @end

p.s. I honestly searched for the answer here and in Google, but found none.

stereojump
  • 21
  • 1

0 Answers0