So i have a Document that's tab based (think of it like the xcode tabs) using and sharing one API-Core object between them. If i've closed it with 2 Windows, one with tab "A" and one with tab "B" and reopen it should create 2 Window Controllers and restore them with their own values. How can I do this? Until now I tried many things but they don't work. Some solutions lead to having no window, some to having too many windows and some lead to the right amount of windows but the windows itself didn't restore their tabs. My current solution is this: (and creates no window)
When I open a new Document everything works fine. But once I saved it, closed the App and reopen it, no window reopens.
The NSDocument Subclass:
- (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]);
}
}
The NSWindowController Subclass:
-(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];
}