Henry Rillovian Henry Rillovian - 24 days ago 12
Objective-C Question

Remember window open/close in Cocoa

My Cocoa application in objective-c has two windows, A and B, that contain helper functionality. The user might not always want these windows to be open (think brush size or color wheel in Gimp). I want the application to remember whether each of these windows was open or closed between each instantiation of the program. For instance:


  1. User opens the program, and both Windows A and B are open

  2. The user closes window A but not window B

  3. User closes the program

  4. User opens the program

  5. Only window B is open, window A is closed

  6. User opens window A

  7. User closes the program

  8. User opens the program

  9. Both windows B and A are open



Is there a way to remember this type of history in Cocoa? Google has not been my friend in this search.

Answer

You can use NSUserDefaults for this purposes. It's key-value based data structure, which is saves between lunches.

In your example you can do something like this:

[[NSUserDefaults standardUserDefaults] setBool:aPresentedFlag forKey:@"aPresented"];

if ([[NSUserDefaults standardUserDefaults] boolForKey@"aPresented"]) {
//show your aWindow
}