1
我製作了一個帶有自定義視圖的Mac應用程序,該視圖是用於拖放文件的目標。我的視圖已經註冊爲目的地,我已經實現了拖動操作方法。一切工作正常,當我編譯和運行的XCode在App,但是當我從Finder中拖動文件打開。應用程序不工作,我看到控制檯以下錯誤:當應用程序從XCode運行但不作爲獨立應用程序運行時,文件拖放視圖起作用
取消阻力,因爲異常「NSInvalidArgumentException」 (原因'發射路徑不可訪問')在拖動期間被提出 會話
任何人都知道手段或爲什麼發生了什麼?下面是相關代碼:
#import "DragDestinationView.h"
@implementation DragDestinationView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// register for dragging types
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
highlight = NO;
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
if (highlight) {
//highlight by overlaying a gray border
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
[self setWantsLayer:YES];
self.layer.masksToBounds = YES;
self.layer.borderWidth = 10.0f;
[self.layer setBorderColor:[[NSColor grayColor] CGColor]];
}
else {
[self.layer setBorderColor:[[NSColor clearColor] CGColor]];
}
}
#pragma mark - Destination Operations
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
NSPasteboard *pboard = [sender draggingPasteboard];
if ([[pboard types] containsObject:NSFilenamesPboardType]) {
// check if it's a bin
NSString* filePath = [[NSURL URLFromPasteboard: [sender draggingPasteboard]] absoluteString];
if ([[[filePath substringFromIndex:[filePath length] - 4] lowercaseString] isEqualToString:@".bin"]) {
// it's a bin
highlight = YES;
[self setNeedsDisplay:YES];
return NSDragOperationCopy;
}
}
return NSDragOperationNone;
}
- (void)draggingExited:(id <NSDraggingInfo>)sender
{
//remove highlight of the drop zone
highlight=NO;
[self setNeedsDisplay: YES];
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
//finished with the drag so remove any highlighting
highlight=NO;
[self setNeedsDisplay: YES];
return YES;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
if ([sender draggingSource] != self) {
NSString* filePath = [[NSURL URLFromPasteboard: [sender draggingPasteboard]] absoluteString];
filePath = [filePath substringFromIndex:7];
filePath = [filePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"file path = %@",filePath);
// send notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"filePathString" object:filePath];
}
return YES;
}
@end
注:draggingEntered:因爲我喜歡箭頭光標告訴他們可以在這裏將一個文件的用戶的綠色圓圈和加方法返回NSDragOperationCopy。我實際上用掉落的文件正在抓取它的路徑位置。