I’m trying to make usage of Cocoa UI preservation API, but it seems I got stuck 
at the very beginning and even after almost two hours of trying to figure out 
what I’m doing wrong, there’s no progress at all. I’m sensing I’m missing 
something obvious, but I don’t know what that would be.

Anyhow, I’ve created a very simple single window application, with (main) 
window, controlled by its accompanying  (main) window controller. The 
controller implementation is fairly simple:


@implementation MainWindowController

- (instancetype)init
{
    self = [super initWithWindowNibName:@"MainWindow" owner:self];
    
    if (self)
    {
        // Some code here.
    }
    
    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    // Some code here.
}

@end


The application delegate is responsible for preservation and it’s implemented 
exactly like in many examples I’ve found online, as well as in Apple 
documentation:


@interface AppDelegate ()
@property (nonatomic, strong) NSWindowController *mainWindowController;
@end

@implementation AppDelegate

+ (void)restoreWindowWithIdentifier:(NSString *)anIdentifier
                              state:(NSCoder *)aState
                  completionHandler:(void (^)(NSWindow *, NSError 
*))aCompletionHandler
{
    NSWindow *window;
    
    if ([anIdentifier isEqualToString:@"main window"])
    {
        AppDelegate *delegate = [NSApp delegate];
        window = [[delegate mainWindowController] window];
    }
    
    aCompletionHandler(window, nil);
}

- (NSWindowController *)mainWindowController
{
    if (!_mainWindowController)
    {
        _mainWindowController = [[MainWindowController alloc] init];
        
        [[_mainWindowController window] setRestorable:YES];
        [[_mainWindowController window] setIdentifier:@"main window"];
        [[_mainWindowController window] setRestorationClass:[self class]];
    }
    
    return _mainWindowController;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[self mainWindowController] showWindow:self];
}

- (void)applicationWillTerminate:(NSNotification *)aNotification
{
    
}

@end


The window itself (loaded from the nib file “MainWindow.xib” contains just one 
tab view, with four tabs.

When I start the application, the window appears just like it was designed in 
the nib file. Then I resize it and change its position on the screen, as well 
as change selected tab of the tab view. Quitting and relaunching the 
application shows the window just like it was designed in the nib files, not UI 
changes were preserved. The    method never gets executed. Additionally, I 
don’t see any preserved data for the application being saved in the 
“~/Library/Saved Application State/” folder.

Is the code above enough to make this simple UI preservation work, or do I need 
to do something more (and what)?

Thanks in advance.
-- Dragan
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to