简体   繁体   English

如何在控制器运行完毕后从appdelegate运行方法?

[英]How to run a method from appdelegate after a controller has finished running?

The idea is that I hava a custom view where the user can drag and drop one or more files and the controller is able to save the path of files into an array. 我的想法是,我有一个自定义视图,用户可以拖放一个或多个文件,控制器可以将文件路径保存到数组中。

How can I run a method from AppDelegate after the user drops the file in the interface? 用户在界面中删除文件后,如何从AppDelegate运行方法?

I have these files: 我有这些文件:

AppDelegate.h: AppDelegate.h:

#import <Cocoa/Cocoa.h>

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

AppDelegate.m: AppDelegate.m:

#import "AppDelegate.h"

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

    @end

DropView.h: DropView.h:

#import <Cocoa/Cocoa.h>

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

DropView.m: 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

Import AppDelegate.h into DropView.m, and call the method you want to run from the performDragOperation: method. 将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;
}

Where doWhatever: is a method implemented in the app delegate. 哪里做什么:是在app委托中实现的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM