Encoding NSViewController for Lion's user interface resume feature

前端 未结 2 830
感情败类
感情败类 2021-01-05 02:52

Any idea what are the best practices for archiving an NSViewController inside a window for resume (user interface preservation) purposes? I\'ve tried archiving

相关标签:
2条回答
  • 2021-01-05 03:00

    I haven’t messed with restorable state much (Jonathon Mah did it for DL3) but if I were doing this I’d try deleting those two methods and implement +restorableStateKeyPaths, e.g.:

    + (NSArray *)restorableStateKeyPaths;
    {
        return @[@“contentViewController.firstInterestingStateProperty”, @“contentViewController.secondInterestingStateProperty”];
    }
    

    And just see if the machinery handled it all for me.

    + (NSArray *)restorableStateKeyPaths;
    

    Returns a set of key paths, representing paths of properties that should be persistent. The frameworks will observe these key paths via KVO and automatically persist their values as part of the persistent state, and restore them on relaunch. The values of the key paths should implement keyed archiving. The base implementation returns an empty array.

    0 讨论(0)
  • 2021-01-05 03:25

    State restoration works for free on NSView but is ignored on NSViewController even though it implements the methods as a subclass of NSResponder. I suppose that's because the window doesn't know about NSViewControllers that may own some of the views it contains.

    On OS X Yosemite it's supposed to work since NSWindow now has real support for NSViewControllers, but it doesn't in my test cases. I guess it's because one needs to "chain" the NSViewControllers using the new APIs to add / remove them vs creating them on the side and just adding their views directly to the window. The latter is actually required if you want to have your app run on pre-Yosemite systems anyway.

    Here's how to make it always work: simply proxy the restoration APIs calls between NSView and NSViewController.

    Subclass NSView like this:

    @interface GIView : NSView
    @property(nonatomic, weak) GIViewController* viewController;  // Avoid retain-loops!
    @end
    
    @implementation GIView
    
    - (void)setViewController:(GIViewController*)viewController {
      _viewController = viewController;
    }
    
    - (void)encodeRestorableStateWithCoder:(NSCoder*)coder {
      [super encodeRestorableStateWithCoder:coder];
    
      [_viewController encodeRestorableStateWithCoder:coder];
    }
    
    - (void)restoreStateWithCoder:(NSCoder*)coder {
      [super restoreStateWithCoder:coder];
    
      [_viewController restoreStateWithCoder:coder];
    }
    
    @end
    

    And NSViewController like this:

    @interface GIViewController : NSViewController
    @end
    
    @implementation GIViewController
    
    - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
      if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        self.view.viewController = self;  // This loads the view immediately as a side-effect
      }
      return self;
    }
    
    - (void)dealloc {
      self.view.viewController = nil;  // In case someone is still retaining the view
    }
    
    - (void)invalidateRestorableState {
      [self.view invalidateRestorableState];
    }
    
    @end
    

    Now you can call -invalidateRestorableState from the NSViewController subclass and Cocoa, thinking it's talking to an NSView, will automatically call -encodeRestorableStateWithCoder: and -restoreStateWithCoder: on your NSViewController subclass as needed.

    0 讨论(0)
提交回复
热议问题