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?
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.
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.
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
}
}
Set isReleasedWhenClosed
in either the Interface Builder or in code, if needed.
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With