Hello:
I have this code and getting error:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
What can I do to prevent the error? Here is the code:
class ViewController: NSViewController,NSWindowDelegate {
var window: NSWindow!
override func viewDidLoad() {
super.viewDidLoad()
}
func windowShouldClose(sender: AnyObject) -> Bool {
NSApp.hide(nil)
return false
}
override func awakeFromNib() {
super.awakeFromNib()
view.window!.delegate! = self //Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
}
Thanks for help.
Did you get the clarification from Apple reviewer on the exact need ?
I understood it as a way to reopen the window once closed.
The way I would do this with a storyboard (it is really a sketch, need to be tested, probably need to keep a reference of the initial windowController when appDidfinishlaunching to avoid creating multiple instances…)
create a menuItem in File or Window menu: such as 'Reopen window'
in AppDelegate
create an IBAction to open the window
@IBAction func reopenFirstWindow(_ sender: NSMenuItem) {
print("Reopen me")
let storyboard = NSStoryboard(name: "Main", bundle: nil)
if let wc = storyboard.instantiateController(withIdentifier: "FirstWC") as? FirstWindowController {
wc.window!.makeKeyAndOrderFront(wc.window!)
}
}
Connect the menuItem to the IBAction by control-drag from the menuItem to the FirstResponder (yellow) square over the mainmenubar in IB.
select the IBAction reopenFirstWindow
Give FirstWC as identifier for the windowController