如何在没有IBAction的情况下更新标签(NSTextfield)?

时间:2014-04-24 13:44:27

标签: objective-c cocoa drag-and-drop nstextfield awakefromnib

我正在尝试实现一个简单的机制:在删除文件后,将其名称传递给lable。但我无法理解如何做到这一点。这是我的代码: H档:

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

M档案:

 #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。 我老老实实地在谷歌搜索答案,但没有找到答案。

0 个答案:

没有答案