Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a new window with its own ViewController from AppDelegate in Swift

I have made a statusBar application with a drop down. I would like to open a settingsWindow from that dropdown. I have made the settings window with its own ViewController.

The issue is that i can't figure out how to instantiate and show the settingsWindow that i have made. I have tried to follow every thread on the internet without any success.

My Viewcontroller:

class SettingsViewController: NSViewController {
   @IBOutlet var ipAddress: NSTextField!
   @IBOutlet var port: NSTextField!

   @IBAction func connect(sender: AnyObject) {}
   override func viewDidLoad() {
      super.viewDidLoad()
   }
}

My AppDelegate:

class AppDelegate: NSObject, NSApplicationDelegate {
   @IBOutlet var statusMenu: NSMenu!
   var statusItem: NSStatusItem?
   var tcpService: TcpService = TcpService()

   func applicationDidFinishLaunching(aNotification: NSNotification?) {

      let bar = NSStatusBar.systemStatusBar()

      statusItem = bar.statusItemWithLength(20)
      statusItem!.menu = statusMenu
      statusItem!.image = NSImage(byReferencingFile: NSBundle.mainBundle().pathForResource("16*16", ofType: "png"))
      statusItem!.highlightMode = true

      tcpService.initOutputStream("192.168.1.1", Port: 8888)
   }

   func applicationWillTerminate(aNotification: NSNotification?) {
      // Insert code here to tear down your application
   }
   @IBAction func openSettings(sender: AnyObject) {
      // open settings for ip and port optional port
   }
}
like image 766
Mads Gadeberg Avatar asked Sep 25 '14 20:09

Mads Gadeberg


1 Answers

in swift 3:

        var myWindow: NSWindow? = nil
        let storyboard = NSStoryboard(name: "Main",bundle: nil)
        let controller: EditorViewController = storyboard.instantiateController(withIdentifier: "editorViewController") as! ViewController
        myWindow = NSWindow(contentViewController: controller)
        myWindow?.makeKeyAndOrderFront(self)
        let vc = NSWindowController(window: myWindow)
        vc.showWindow(self)
like image 85
Danny Shen Avatar answered Sep 20 '22 02:09

Danny Shen