简体   繁体   中英

Cocoa: Call App Delegate Method from another Class

I'm currently trying to get the path of a file from a drag and drop operation inside of a custom view, and then pass that path to my app delegate. I'm currently using the following:

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender 
{

    NSPasteboard *pb = [sender draggingPasteboard];
    NSString *type = [pb availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]];
    NSArray *array = [[pb stringForType:type] propertyList];

    //access the app delegate
    NSApplication *myApplication;
    myApplication = [NSApplication sharedApplication];

    [myApplication uploadFiles:array];

    return NO;

} 

However, I keep getting a message that says that my app delegate doesn't respond to the "uploadFiles" method. It is declared inside of my app delegate. Am I accessing the NSApplication in the correct manner?

Thanks.

I believe the problem is that you're referring to the application but not its delegate. This should work:

Mac

[(YourAppDelegate *)[[NSApplication sharedApplication] delegate] uploadFiles:array]

replacing YourAppDelegate with your actual app delegate name, and being certain to #import it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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