使用多个窗口控制器还原文档

时间:2014-09-05 12:10:12

标签: cocoa nsdocument nswindowcontroller

所以我有一个基于标签的文档(想象它像xcode标签)在它们之间使用和共享一个API-Core对象。如果我用2个Windows关闭它,一个带有标签“A”,一个带有标签“B”并重新打开它应该创建2个窗口控制器并用它们自己的值恢复它们。我怎样才能做到这一点? 直到现在我尝试了很多东西,但它们不起作用。一些解决方案导致没有窗口,一些解决方案导致窗口太多而一些导致适当数量的窗口,但窗口本身并没有恢复其标签。我目前的解决方案是:(并且不创建窗口)

当我打开一个新文档时,一切正常。但是一旦我保存它,关闭应用程序并重新打开它,没有窗口重新打开。

NSDocument子类:

- (id)init
{
    self = [super init];
    if (self) {
        // Add your subclass-specific initialization here.
        documentViewControllers = [[NSMutableArray alloc] init];
    }
    return self;
}

-(void)makeWindowControllers {
    if(core == nil) {
        core = [[TestCore alloc] init];
        DocumentWindowController *dwc = [[DocumentWindowController alloc] initWithWindowNibName:@"DocumentWindowController"];
        [dwc setCore:core];
        [dwc showWindow:self];
        [self addWindowController:dwc];
    }
}


-(void)encodeRestorableStateWithCoder:(NSCoder *)coder {
    [super encodeRestorableStateWithCoder:coder];
    //documentWindowControllers is an additional array storing the controllers
    [coder encodeInteger:[documentWindowControllers count] forKey:@"windowCount"];
}

-(void)restoreStateWithCoder:(NSCoder *)coder {
    [super restoreStateWithCoder:coder];
    NSInteger count = [coder decodeIntegerForKey:@"windowCount"];
    NSLog(@"Restore with window count: %ld",(long)count);
    for (NSInteger i = 0; i < count; i++) {
        DocumentWindowController *dwc = [[DocumentWindowController alloc] initWithWindowNibName:@"DocumentWindowController"];
        [dwc setCore:core];
        [dwc showWindow:self];
        [self addWindowController:dwc];
    }
}

-(void)addWindowController:(NSWindowController *)windowController {
    [super addWindowController:windowController];
    if([windowController isKindOfClass:[DocumentWindowController class]]) {
        [(DocumentWindowController*)windowController setCore:core];
        [documentViewControllers addObject:windowController];
        NSLog(@"Existing controllers: %ld",[documentViewControllers count]);
    }
}

-(void)removeWindowController:(NSWindowController *)windowController {
    [super removeWindowController:windowController];
    if([windowController isKindOfClass:[DocumentWindowController class]]) {
        [documentViewControllers removeObject:windowController];
        NSLog(@"Existing controllers: %ld",[documentViewControllers count]);
    }
}

NSWindowController子类:

-(void)encodeRestorableStateWithCoder:(NSCoder *)coder {
    NSLog(@"Encode Window");
    //code to save the tabs
}

-(void)restoreStateWithCoder:(NSCoder *)coder {
    NSLog(@"Restoring");
    //code to restore the tabs
}

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];

    // liveViewControllers are the view controllers for every tab
    if(liveViewControllers == nil) {
        liveViewControllers = [NSMutableArray array];
    }

    [tabBar setStyleNamed:@"Card"];
    [tabBar setOnlyShowCloseOnHover:YES];
    [tabBar setCanCloseOnlyTab:NO];
    [tabBar setAllowsBackgroundTabClosing:YES];
    [tabBar setHideForSingleTab:NO];
    [tabBar setShowAddTabButton:YES];
    [tabBar setButtonMinWidth:100];
    [tabBar setButtonMaxWidth:280];
    [tabBar setButtonOptimumWidth:130];
    [tabBar setSizeButtonsToFit:NO];
    [tabBar setTearOffStyle:MMTabBarTearOffMiniwindow];
    [tabBar setUseOverflowMenu:YES];
}

0 个答案:

没有答案