Reputation: 332
I have different sections to my application. I want to have buttons change the view. So, if I click the first button the windows's view will change to another view. Is there anyway to do this?
Upvotes: 0
Views: 875
Reputation: 22958
Updated: Sample project that uses this code:
You can use the following categories on NSWindow
to implement switching from one view to another.
MDAppKitAdditions.h:
@interface NSWindow (MDAdditions)
- (CGFloat)toolbarHeight;
- (void)resizeToSize:(NSSize)newSize;
- (void)switchView:(NSView *)aView title:(NSString *)aString;
- (void)switchView:(NSView *)aView;
@end
MDAppKitAdditions.m:
static NSView *blankView() {
static NSView *view = nil;
if (view == nil) view = [[NSView alloc] init];
return view;
}
@implementation NSWindow (MDAdditions)
- (CGFloat)toolbarHeight {
NSToolbar *toolbar = self.toolbar;
CGFloat toolbarHeight = 0.0;
if (toolbar && toolbar.isVisible) {
NSRect windowFrame = [[self class] contentRectForFrameRect:self.frame
styleMask:self.styleMask];
toolbarHeight = NSHeight(windowFrame) - NSHeight([self.contentView frame]);
}
return toolbarHeight;
}
- (void)resizeToSize:(NSSize)newSize {
CGFloat newHeight = newSize.height + [self toolbarHeight];
CGFloat newWidth = newSize.width;
NSRect aFrame = [[self class] contentRectForFrameRect:self.frame
styleMask:self.styleMask];
aFrame.origin.y += aFrame.size.height;
aFrame.origin.y -= newHeight;
aFrame.size.height = newHeight;
aFrame.size.width = newWidth;
aFrame = [[self class] frameRectForContentRect:aFrame
styleMask:self.styleMask];
[self setFrame:aFrame display:YES animate:YES];
}
- (void)switchView:(NSView *)aView title:(NSString *)aTitle {
if (self.contentView != aView) {
[self setContentView:blankView()];
if (aTitle) [self setTitle:NSLocalizedString(aTitle, @"")];
[self resizeToSize:aView.frame.size];
[self setContentView:aView];
}
}
- (void)switchView:(NSView *)aView {
return [self switchView:aView title:nil];
}
@end
To use it, I assume you would have a controller class like the following, with IBOutlets for views and your main window:
@interface MDAppController : NSObject {
IBOutlet NSWindow *mainWindow;
IBOutlet NSView *firstView;
IBOutlet NSView *secondView;
}
- (IBAction)changeView:(id)sender;
@end
Then in your implementation, something like this:
#import "MDAppKitAdditions.h"
@implementation MDAppController
- (IBAction)changeView:(id)sender {
NSInteger tag = [sender tag];
NSView *targetView = nil;
if (tag == 0) {
targetView = firstView;
else if (tag == 1) {
targetView = secondView;
}
[mainWindow switchView:targetView title:@"New window title"];
}
@end
You could set it up so that the buttons you want to use to switch views each call the same changeView:
method instead of defining separate methods for each one. In the nib file in Interface Builder, you can set the tag property of the buttons to differentiate between them. At runtime, when you click the button and it calls the changeView:
method, the button is passed in as the generic sender
parameter, so you then examine that to determine which view you should switch to.
Upvotes: 2
Reputation: 18132
Use an NSTabView. You can make the tab view borderless so that no bezel or tabs are drawn (using [tabView setTabViewType:NSNoTabsNoBorder]
) and then implement your own button actions to change the views programatically by switching the active tab using -selectTabViewItem:
or -selectTabViewItemAtIndex:
. This has the added benefit of being able to configure all of your views in Interface Builder.
Upvotes: 2