Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

User Interface Preservation

BrianSlickBrianSlick Posts: 9,398Tutorial Authors, Registered Users
edited August 2012 in Mac OS X Development
Client requirement is that the user should come back to wherever they were when they quit the app. Ok, I suppose that is inline with some of the newer iOS-style backgrounding behaviors, so time to roll up my sleeves and make it happen.

I'm really having trouble figure out what is supposed to happen automatically (if anything), and what I need to do manually. The only information I can find is this:

CoreAppDesign

...which is nearly worthless because it doesn't provide any tangible steps to take. From the description and the diagram, a NSWindow object will save its corresponding NSWindowController, and its NSViews. That's great, except that my window controller may have properties that also need to be saved, and my views all have NSViewControllers that will have their own data that needs to be saved. I expect I need to manually do stuff, and that's fine, but what I can't figure out is where or when.

For my primary window, I have specified that it is restorable (both in IB and in code). I have made sure that the window has both an identifier and an autosave name (both in IB and in code). I have specified the app delegate as the restoration class, then I do the class method as shown in Listing 2-2. The first problem is that this method hardly even seems to get called. Maybe one out of 5 or 10 launches, and I can't figure out a pattern.

In the NSWindowController, as far as I can tell I have 2 options. I can implement these NSResponder methods:
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
- (void)restoreStateWithCoder:(NSCoder *)coder
and/or I can implement these NSWindowDelegate methods:
- (void)window:(NSWindow *)window willEncodeRestorableState:(NSCoder *)state
- (void)window:(NSWindow *)window didDecodeRestorableState:(NSCoder *)state
In either case, seems like I would encode/decode any properties and view controllers at this time. I haven't seen the NSResponder methods get called yet under any circumstances. The NSWindowDelegate ones get called semi-regularly, but I haven't quite figured out what to do there. For example, in the encode one, I do this:
- (void)window:(NSWindow *)window
willEncodeRestorableState:(NSCoder *)state
{
[state encodeObject:[self splitViewController] forKey:@"SplitViewController"];
}
This method runs, but the NSCoding methods in the splitViewController never run. So if something is being encoded, I can't tell. When I go to decode:
- (void)window:(NSWindow *)window
didDecodeRestorableState:(NSCoder *)state
{
if ([state containsValueForKey:@"splitViewController"])
{
[self setSplitViewController:[state decodeObjectForKey:@"SplitViewController"]];
}
}
...the test passes, so there is something there, but if I check the property value after decoding, it is nil. Even if it wasn't nil, this view controller is not the same one that gets created in windowDidLoad, so it seems like there would be a disconnect between this view controller and the (presumably) automatically restored view.

I added this to the window controller:
+ (NSArray *)restorableStateKeyPaths
{
return [NSArray arrayWithObjects:@"splitViewController", nil];
}
...and it does get called pretty reliably at launch, but I can't tell what it's doing for me. Seems like these things should have save/load pairs, and I'm not really understanding what the opposite method of this is, if there is one.

If I need to roll my own system with notifications being posted and manually encoding/decoding, I guess I can do that but it will be a giant PITA. Can anybody describe what the process needs to be, and/or direct me to a tutorial? I'm really not finding anything via Google.
Sign In or Register to comment.