我对OSX应用程序开发完全陌生,从状态栏菜单打开新窗口时遇到一些问题。
我读了几本教程,并努力学习。因为它们不完全适合我的项目,我做了一些修改,但似乎不起作用。
为了设计一些UI,我创建了一个新的nswindowcontroller子类。但是当我点击菜单时,没有窗口出现。

//AppDelegate.Swift

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)

    @IBOutlet weak var window: NSWindow!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
        if let button = statusItem.button {
            button.image = NSImage(named: "StatusBarButtonImage")
        }

        let menu = NSMenu()

        menu.addItem(NSMenuItem(title: "About MyProgram", action: Selector("getAbout:"),keyEquivalent: ""))

        statusItem.menu = menu

    }

    // open "About MyProgram" window
    func getAbout(sender: AnyObject) {
        let controller = AboutWindowController()
        controller.showWindow(self)
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }


}

我不知道在这个新类中写什么,所以我把它保留为默认创建的。
//AboutWindowController.swift
import Cocoa

class AboutWindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    }

}

感谢您的帮助!谢谢!

最佳答案

像这样工作,像这样编码

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate {
    NSWindowController* wc;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    NSStoryboard* storyboard = [NSStoryboard storyboardWithName:@"Main"
                                                      bundle:nil];
    wc = [storyboard instantiateControllerWithIdentifier:@"new_window"];
    [wc showWindow:self];
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

@end

我想你可以把objective-c语法改成swift语法。

10-05 19:47