Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSViewController-Close window-Swift-Storyboard

Tags:

xcode

swift

cocoa

I cannot find the right way to close the main window after I clicked the "proceed" button installed on this window. I have try to connect this button to the "proceedclose" option of the inspector. I have also try to insert the following line inside my code :

import Cocoa
class ViewController: NSViewController {
    ...
    @IBAction func Envoi(sender: NSButton) {    
        self.view.window!.close()
    }
}

None of them works, nothing happens and no error is reported. Could anyone help me to sort it out?

like image 932
Fredo Avatar asked Nov 08 '15 13:11

Fredo


3 Answers

I had same question. And your code was very helpful for me.

Then I checked "VC <- view <- window <- windowController" out in NS Lib. hierarchey has.

When you close window from VC, avoiding storyboard grow bizzare. you can solve this, by

self.view.window?.windowController?.close()

Just a quick note.

like image 64
Set Minami Avatar answered Oct 02 '22 20:10

Set Minami


It is working by using "self.view.window!.close()" inside the button action, but when I add a new ViewController, and link the button to it, the first window does not disappear anymore.

I'm assuming you may have forgotten to duplicate the @IBAction func Envoi(sender: NSButton) in the new ViewController? By doing this only your new window will close and not the first window.

like image 41
SammyRNYCreal Avatar answered Oct 02 '22 19:10

SammyRNYCreal


  1. Be aware of each NSViewController and NSButton instance. Fix management of NSViewController and NSButton instances if needed.
class ViewController: NSViewController {
    @IBOutlet weak var envoiButton: NSButton!
    //…
    @IBAction func doEnvoiButtonPressed(_ sender: NSButton) {    
        print(self.hash)
        print(self.envoiButton.hash)
        // … other code
    }
}
  1. Set isReleasedWhenClosed in either the Interface Builder or in code, if needed.

  2. Closed the window via the NSButton @IBAction (e.g. Envoi) with one of the following

  • self.view.window!.windowController!.close() NSWindowController close() abrupt close without possibility to ask user for confirmation.
  • self.view.window!.close() NSWindow close() sends willCloseNotification notification.
  • self.view.window!.performClose(nil) or self.view.window!.performClose(self) NSWindow performClose(_:) sends to all window closing signals. First, windowShouldClose(_:) to window delegate if present. If no delegate is present, then invokes any NSWindow object windowShouldClose(_:) implementation. If windowShouldClose(_:) returns true or is not implemented, then NSWindow close() is called. (performClose(_:) is the more comprehensive and flexible choice)
class ViewController: NSViewController {
    @IBAction func doEnvoiButtonPressed(_ sender: NSButton) {    
        self.view.window!.performClose(nil) // or performClose(self)
    }
}
like image 38
l --marc l Avatar answered Oct 02 '22 18:10

l --marc l