简体   繁体   中英

Show NSOpenPanel in a NSStatusItem app

I am writing a status item app that does not have an NSWindow. I to pull up an NSOpenPanel when the user clicks on the status item. How would one do this when the app does not utilize an NSWindow?

Thanks.

将其作为模式窗口而不是工作表运行。

In your status item's IBAction method, call this:

window = [[NSApp currentEvent] window];

You can then pass that window to NSOpenPanel's beginSheetModalForWindow:completionHandler: in order to display the open panel as a sheet.

You may find that the status item itself curls up and disappears as the sheet appears, but it reappears when you dismiss the sheet.

You can simply call your open panel from NSMenuItem's action as:

NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setAllowsMultipleSelection:YES];
    [panel setCanChooseDirectories:YES];

    NSUInteger result = [panel runModal];
    NSMutableArray *paths = [NSMutableArray array];

    if(result == NSFileHandlingPanelOKButton) {
        for (NSURL *url in [panel URLs]) {
            NSLog(@"%@", url);
        }
    }

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