Why fatal error with call awakeFromNib?

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.

Answered by Claude31 in 412309022

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

Where is window set ?

If you test you will see view.window is nil


It is defined as implicit unwrapped, but initial value is nil as long as you have not set it.


Do you use a storyboard ?

Then you should get a windowController and its ViewController, of type ViewController.

If so, why do you need awake ? Why setting the delegate ?


Or did you create a nib for the window ?

Hello Claude:


I do have a storyboard. I am still trying to resolve the issue of restoring a window after it has been closed. The effort here is to hide the window when the user clicks close so that the window can be restored.


Frankly I am now at my wits end!

Accepted Answer

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

Hello Claude:


I found a way to open the window after it was closed (with the red x button) using a modal storyboard segue from the menu link. To quit after it was hooked up this way I had to use an @IBOutlet as follows:


@IBActionfunc closeApplication(_ sender: NSMenuItem) {
       
        NSApplication.shared.terminate(AppDelegate.closeApplication)
        print("Stop the App")
    
    }


Without that it wouldn't close.

Thanks for your great help during this difficult journey. That was the last hurdle to jump over.

Wish you the best now with the review board.

Hello:


As I was reviewing everything before submission I ran into an inexplicable problem:


When I Close the Main Window by clicking the red x and reopen it, then I load another window from a link on the main window and try to load another window, The Main window becomes unusable. I have to close it then reopen it to click on the links. Then when I open another window, the previous window I opened becomes unusable until I close the Main Window.


To have access to more that one window at a time I have to close the Main Window.


Have you seen anythink like this?

After some experimenting,


I found that making the segues "show" instead of "modal" I can open all the windows initially and still access the Main Window.

However, When I close the Main Window with the close red x, and reopen it, I can launch all the windows but after launching them I have to close it to use the windows.

Why fatal error with call awakeFromNib?
 
 
Q