I\'m creating an OSX application, which has one window. This window contains a single view, which presents different views throughout usage.
My current approach:
The project is on Github https://github.com/MacUserT?tab=repositories.
I also added the app in the repository, so you can run it straight from your folder. If you download the project, you should be able to open it in xcode, compile it and run it. You should get the same result as the app I uploaded.
I hope this helps and works.
Kind regards, MacUserT
Your mistake is that you didn't initially displayed your first view. Here is the way I learned how to swap views in and out of a window. Whether it is the best way, is not up to me. The difference with your approach is that you should decouple the views from your window. There might be better ways, but the following approach is officially taught.
Use your mainmenu.xib als the window controller. Drop a box in the window and set the box border to None. You will have an invisible box that will be the container of your views.
Create as many viewcontrollers as you want to have different views. This by, of course, creating a new class as a subclass of NSViewController. In your main class (perhaps appDelegate, but it can be some other class) you at least create an IBOutlet of NSBox (the box you droppend in the main window) and an array of viewcontrollers. The later will contain all the views you want to display.
Here is an easy sample to show you how it works:
The appDelegate.h contains these declarations:
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (weak) IBOutlet NSButton *swapViewButton;
@property (weak) IBOutlet NSBox *viewBox;
@property NSMutableArray *viewControllers;
@property NSUInteger currentView;
-(IBAction)swapViewOnButtonclick:(id)sender;
-(void)displayViewController:(NSViewController *)vc;
@end
The appDelegate.m contains these methods:
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@end
@implementation AppDelegate
@synthesize viewControllers;
@synthesize currentView;
-(id)init
{
self = [super init];
if(self)
{
viewControllers = [[NSMutableArray alloc]init];
NSViewController *vc;
vc = [[SecondViewController alloc]init];
[viewControllers addObject:vc];
vc = [[FirstViewController alloc]init];
[viewControllers addObject:vc];
}
NSLog(@"The number of views in the view controller array is: %ld.\n", [viewControllers count]);
return self;
}
-(void)displayViewController:(NSViewController *)vc
{
NSWindow *w = [_viewBox window];
BOOL ended = [w makeFirstResponder:w];
if(!ended)
{
NSBeep();
return;
}
NSView *v = [vc view];
[_viewBox setContentView:v];
}
-(IBAction)swapViewOnButtonclick:(id)sender
{
if(!currentView)
{
currentView++;
}else
{
currentView = 0;
}
NSLog(@"Current view is: %ld.\n", currentView);
NSViewController *vc = [viewControllers objectAtIndex:currentView];
[self displayViewController:vc];
}
-(void)awakeFromNib
{
currentView = 0;
[self displayViewController:[viewControllers objectAtIndex:currentView]];
}
@end
The view controllers are similar and just have an initializer in the sample. I droppend a text label on the view with the text "first/second view". Of course, this is just an example how view swapping works. You can make it as complicated as necessary for your app. Hope this helps.
MacUserT