【问题标题】:Simple Drag and Drop into Window returning file path简单拖放到窗口返回文件路径
【发布时间】:2012-07-18 19:33:20
【问题描述】:

我想在我的应用程序中实现简单的拖放。 如果您将文件拖到我想返回的窗口中 带有 NSLog 的文件路径。这是我的代码,但是 如果我拖动文件,什么也不会发生。顺便说一句,我 将 AppDelegate 与引用插座连接 使用窗口(委托)从窗口接收所有内容。

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

【问题讨论】:

  • 这不是 Xcode 问题。

标签: objective-c cocoa xcode4.3


【解决方案1】:

只有占据屏幕空间的对象——窗口或视图——才能接受和处理拖动事件。您的应用程序委托都不是这些。此外,窗口不会将它收到的任何消息发送给它的委托。它只发送属于NSWindowDelegate 协议的消息。你需要在一个视图类中实现这个拖动代码,它的一个实例出现在屏幕上。

【讨论】:

    【解决方案2】:

    这是一个老问题,但我是这样解决这个问题的:

    我创建了NSWindow 的子类,将其命名为MainWindow。然后我将协议NSDraggingDestination 添加到新类MainWindow 中。在 Interface Builder 中,我选择了应用程序窗口,并将 MainWindow 作为类名放在 Identity Inspector 中。

    AppDelegate

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        [_window registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
    }
    

    以及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
    

    像魅力一样工作! (终于!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 2020-02-22
      • 2012-03-28
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多