0

这个想法是我有一个自定义视图,用户可以在其中拖放一个或多个文件,并且控制器能够将文件路径保存到数组中。

用户在界面中放置文件后,如何从 AppDelegate 运行方法?

我有这些文件:

AppDelegate.h:

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSScrollView *table;
@property (assign) IBOutlet NSWindow *window;
@end

AppDelegate.m:

#import "AppDelegate.h"

    @implementation AppDelegate
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        // Insert code here to initialize your application
    }

    @end

DropView.h:

#import <Cocoa/Cocoa.h>

@interface DropView : NSView <NSDraggingDestination>
@property (assign) IBOutlet NSScrollView *table;
@property NSArray *draggedFilePaths;
@end

DropView.m:

#import "DropView.h"

@implementation DropView
@synthesize draggedFilePaths;

- (id)initWithFrame:(NSRect)frame
{

    self = [super initWithFrame:frame];
    if (self) {
        [self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]];
    }

    return self;
}

-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender{
    return NSDragOperationGeneric;
}

-(NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender{
    return NSDragOperationCopy;
}
-(BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender{
    return YES;
}

-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
    NSPasteboard* prb;
    prb= [sender draggingPasteboard];
    draggedFilePaths = [prb propertyListForType:NSFilenamesPboardType];
    return YES;
}

- (void)concludeDragOperation:(id<NSDraggingInfo>)sender{
    [self setNeedsDisplay:YES];
    NSLog(@"path %@",draggedFilePaths);
    [self populateTable];
}

- (void)drawRect:(NSRect)dirtyRect
{
}

-(void)populateTable{
    NSLog(@"yes");
}

@end
4

1 回答 1

2

将 AppDelegate.h 导入 DropView.m,并从 performDragOperation: 方法中调用您要运行的方法。

-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
    NSPasteboard* prb;
    prb= [sender draggingPasteboard];
    draggedFilePaths = [prb propertyListForType:NSFilenamesPboardType];
    [(AppDelegate *)[[NSApplication sharedApplication]delegate] doWhatever:draggedFilePaths]; 
    return YES;
}

其中 doWhatever: 是在应用程序委托中实现的方法。

于 2012-11-11T05:41:21.067 回答