6

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
   }
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
Mads Gadeberg
  • 1,429
  • 3
  • 20
  • 30

4 Answers4

15

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)
Danny Shen
  • 147
  • 1
  • 4
  • Why not instantiate a window controller directly from the storyboard and skip the window setup? –  Dec 14 '20 at 16:55
0

For 2022

  1. in your normal Main storyboard, tap to add a new window controller.

enter image description here

  1. tap precisely on the red "X", then the blue circle, and then enter "ExampleID" at the green entry.

  2. in your app's ordinary main view controller, add this

variable:

var otherWindow: NSWindowController?

function:

private func otherWindow() {
    let sb = NSStoryboard(name: "Main", bundle: nil)
    otherWindow = sb.instantiateController(
       withIdentifier: "ExampleID") as! NSWindowController
    otherWindow?.showWindow(self)
}

That's it.

Call otherWindow when you want to.

Problem:

Inevitably you will want to set up the otherWindow in a certain way, example, transparent, whatever. Unfortunately this is a whole topic in itself, but you do it like this:

private func otherWindow() {
    ... as above ...
    otherWindow?.window?.ExampleSetup()
}

and then

extension NSWindow {
    func ExampleSetup() {
        self.styleMask = .borderless
        self.collectionBehavior = [.fullScreenPrimary]
        self.level = .floating
        self.isMovable = false
        self.titleVisibility = .hidden
        // etc etc etc ..
        guard let screen = self.screen ?? NSScreen.main else {
            print("what the???")
            return
        }
        self.setFrame(screen.frame, display: true)
        // consider also .visibleFrame
    }
}
DharmanBot
  • 1,066
  • 2
  • 6
  • 10
Fattie
  • 27,874
  • 70
  • 431
  • 719
0
enum Storyboards: String {
case main = "Main"

func instantiateVC<T>(_ identifier: T.Type) -> T?  {
    let storyboard = NSStoryboard(name: rawValue, bundle: nil)
    guard let viewcontroller = storyboard.instantiateController(withIdentifier: String(describing: identifier)) as? T else { return nil}
    return viewcontroller
   }
}


var ssoLoginController: IDSSOLoginViewController?
var myWindow: NSWindow? = nil

ssoLoginController = Storyboards.main.instantiateVC(IDSSOLoginViewController.self)
myWindow = NSWindow(contentViewController: ssoLoginController!)
myWindow?.makeKeyAndOrderFront(self)
let vc = NSWindowController(window: myWindow)
vc.showWindow(self)
Mannam Brahmam
  • 2,225
  • 2
  • 24
  • 36
-1

I am not 100% that I fully understand your problem, but assuming that you are using a storyboard (you should if you are starting fresh), adding few lines to your applicationDidFinishLaunching method will help:

        var myWindow: NSWindow? = nil
        let storyboard = NSStoryboard(name: "Main",bundle: nil)
        let controller: SettingsViewController = storyboard?.instantiateControllerWithIdentifier("SettingsViewController") as SettingsViewController
        myWindow = controller.window
        myWindow?.makeKeyAndOrderFront(self)

Do not forget to set the Storyboard ID in IB (in the example above to SettingsViewController)!

Georg Tuparev
  • 441
  • 2
  • 6
  • 11
  • 2
    I don't think this code works. SettingsViewController does not have a member named 'window'. Any suggestions please? – iphaaw Jun 19 '15 at 12:16