chrono chrono - 5 months ago 31
Swift Question

windowShouldClose reporting unresolved identifier

I'm trying to trigger a save of my CoreData on a window close as its only a single window application.

I've got the following code in my viewDidLoad and viewDidAppear

override func viewDidLoad() {
super.viewDidLoad()
if windowShouldClose(self) {
saveValues()
}

}

override func viewDidAppear() {
super.viewDidAppear()
self.view.window?.delegate = self
}


however im still getting the following error

Use of unresolved identifier 'windowShouldClose'


Any advice as to why i'm still getting this error after declaring the window delegate as self?

Answer

Set the delegate in viewDidLoad

override func viewDidLoad() {
   super.viewDidLoad()
   self.view.window?.delegate = self
}

and implement the delegate method

func windowWillClose(notification: NSNotification) {
   saveValues()
}

windowShouldClose is different. It asks for permission to close the window and expects a boolean return value.