0

I'm developing a macOS application using cocoa with objective-C. I want to implement a function which could close a NSDocument directly without showing NSSavePanel. I use [self close] in my subclass of NSDocument, but after the window closed, I could not open other NSDocument any more (and there is no error information).

Marek H
  • 5,173
  • 3
  • 31
  • 42
Evian
  • 1,035
  • 1
  • 9
  • 22
  • If you use autosave + true NSCoding your mac can reboot and user won't lose data. The save dialog only appears when user did a change to document and he manually clicks close (document dirty means updateCount method was called manually or by nsundomanager). Just avoid calling updateCount (or make document dirty). You can even override updateCount. Doing this you lose ton of natural behaviour and your app doesn't solve data store when reboot/power outage ... – Marek H Mar 05 '19 at 08:49
  • @MarekH What I want to do is handle the close button. In my project, the red close button is clicked without any change, nothing appears. To make the function of 'Delete' button in the save panel that may have appeared when close button is clicked, I need to close the document without saving. – Evian Mar 05 '19 at 09:51
  • Why do you need to use NSDocument if you don't want to use it's functionality? Just show windowcontroller and you are done. It's not the best idea to fight the frameworks. – Marek H Mar 05 '19 at 10:27
  • Read section "The Document Architecture Provides Many Capabilities for Free" https://developer.apple.com/library/archive/documentation/General/Conceptual/MOSXAppProgrammingGuide/CoreAppDesign/CoreAppDesign.html + Shoebox-Style Apps Should Not Use NSDocument – Marek H Mar 05 '19 at 10:29
  • @MarekH Thank you. From the article, it seems that NSDocument has its own close mechanism. But when I press the red close button of my application, it just stuck and nothing happens. Is there any notification representing the close button is clicked for me to debug? – Evian Mar 05 '19 at 10:32
  • NSWindowDelegate : - (BOOL)windowShouldClose:(NSWindow *)sender; or - (void)windowWillClose:(NSNotification *)notification; you should handle those in your delegate (either subclass of nswindowcontroller or any other object). PS: NSDocument creates it's own windowcontroller on background (you need to set delegate to nsdocument object). You can control creation of windowController by overriding makewindowcontrollers look here https://stackoverflow.com/questions/32316958/how-to-use-nsviewcontroller-in-an-nsdocument-based-cocoa-app/32328975#32328975 – Marek H Mar 05 '19 at 13:33

0 Answers0