简体   繁体   English

NSApplicationDelegate应用程序:openFile。 永远不会到达openFile:功能

[英]NSApplicationDelegate application:openFile. Never reaches to openFile: function

I want to open a file dropping it on the app icon. 我想打开一个文件,将其放在应用程序图标上。 When I do it my app is opened so the file extension is well defined and related to my app. 当我这样做时,我的应用程序被打开,因此文件扩展名已明确定义并与我的应用程序相关。 But the application:openFile: function never is called. application:openFile:函数永远不会被调用。 so I can't open the file dropped in my app. 所以我无法打开我的应用程序中丢弃的文件。 I traced openFile: but never goes. 我追踪openFile:但是永远不会去。

All the answers that I found are just to add in the delegate the openFile: and that's all but not in my case. 我找到的所有答案只是在委托中添加openFile:这就是我的情况所不同的。

Any help will be very appreciate it. 任何帮助都会非常感激。 Thanks a lot in advance. 非常感谢提前。

This is my environment. 这是我的环境。

The plist has got the extension of files to be opened. plist已经获得了要打开的文件的扩展名。 My app is opened when I drop the files. 我删除文件时打开了我的应用程序。

I initialize my delegate at the beggining of the app, 我在app的开始时初始化我的代表,

   mydelegate = [[MyController alloc] init];

And in the delegate, 在代表中,

in the include, 在包括中,

@interface MyController : NSObject <NSApplicationDelegate> {

@private

     NSWindow *window;
}

@property (assign) IBOutlet NSWindow *window;

-(id)   init;

-(BOOL) application: (NSApplication*)sharedApplication openFile:(NSString*) fileName;

@end

And in the .m file, 在.m文件中,

@implementation MyController

@synthesize window;


- (id)init{

    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(applicationWillFinishLaunching:)
          name:NSApplicationWillFinishLaunchingNotification object:nil];
    }
    return self;
}

- (void) applicationWillFinishLaunching:(NSNotification *)aNotification{

    NSLog(@"applicationWillFinishLaunching");
}

-(BOOL) application: (NSApplication*)sharedApplication openFile:(NSString*) fileName {

    NSLog(@"openFile=%@", fileName);
    return YES;
}

@end

At least in the code provided above, you are not explicitly setting the app's delegate to be an instance of MyController . 至少在上面提供的代码中,您没有明确地将应用程序的委托设置为MyController的实例。 Are you setting the delegate anywhere? 你在任何地方设置代表吗?

Immediately following [[MyController alloc] init] , try this: 紧接着[[MyController alloc] init] ,试试这个:

[[NSApplication sharedApplication] setDelegate: mydelegate];

Without making this connection, the app won't know who is supposed to handle delegate responsibilities. 如果不进行此连接,应用程序将无法知道谁应该处理委派职责。

OR 要么

The most common way to handle drag and drop onto the dock icon is to simply implement: 处理拖放到停靠栏图标的最常用方法是简单地实现:

-(BOOL)application:(NSApplication *)sender openFile:(NSString *)path

as part of the AppDelegate class that is auto-generated for you by Xcode when you start a project. 作为AppDelegate类的一部分,当您启动项目时,Xcode会自动为您生成该类。

Include this one line in your method where you are getting filePath:- 在您获取filePath的方法中包含这一行: -

//Include this line for calling your method
[self application:[NSApplication sharedApplication]openFile:filename];

If you have an AppleEvent event handler listening to for 'odoc' Open Document apple events: 如果你有一个AppleEvent事件处理程序监听'odoc' Document苹果事件:

NSAppleEventManager.shared().setEventHandler(self,
                                             andSelector: #selector(handle(event:replyEvent:)),
                                             forEventClass: AEEventClass(kCoreEventClass),
                                             andEventID: AEEventID(kAEOpenDocuments))

Then the handler will intercept the calls and the normal App Delegate methods will not be called. 然后处理程序将拦截调用,并且不会调用正常的App Delegate方法。

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

相关问题 应用程序:启动画面后未调用 OpenFile - Application:OpenFile Not Called After Splash Screen application:openFile:在启动时不会被调用 - application:openFile: doesn't get called at launch application:openFile:尝试打开自己的文件类型时报告错误 - application:openFile: did report error when trying to open own file type 在PyObjC中为openFile实现NSApplication委托协议 - Implementing NSApplication delegate protocol for openFile in PyObjC 为什么从未调用过我的NSApplicationDelegate类中的applicationWillFinishLaunching? - Why is applicationWillFinishLaunching in my NSApplicationDelegate class never called? NSWorkspace openFile:将焦点随机带回我的应用程序 - NSWorkspace openFile: randomly brings focus back to my app 由于URL协议,NSApplicationDelegate应用程序处于活动状态 - NSApplicationDelegate application active because of URL Protocol 将可编写脚本的属性添加到从 NSObject 派生的 Cocoa 应用程序<nsapplicationdelegate> ?</nsapplicationdelegate> - Add scriptable property to Cocoa application derived from NSObject <NSApplicationDelegate>? 如果NSFileManager从未到达释放消息,则将其释放 - Release NSFileManager if it never reaches the release message Cocoa:将参数传递给NSApplicationDelegate - Cocoa: Pass arguments to NSApplicationDelegate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM