2013-02-04 86 views
0

我对我的应用程序有不同的部分。我想让按钮改变视图。所以,如果我点击第一个按钮,窗口的视图将变为另一个视图。无论如何要做到这一点?NSWindow集合视图

回答

2

使用NSTabView。您可以使选项卡视图无边界,以便不绘制边框或选项卡(使用[tabView setTabViewType:NSNoTabsNoBorder]),然后执行自己的按钮操作,通过使用-selectTabViewItem:-selectTabViewItemAtIndex:切换活动选项卡以编程方式更改视图。这具有能够在Interface Builder中配置所有视图的附加好处。

+1

您也可以在IB中设置选项卡视图类型。 –

2

更新:使用此代码示例项目:

SwitchView.zip

enter image description here

您可以使用NSWindow以下几类实现从一个视图切换到另一个。

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 

要使用它,我认为你将有一个控制器类像下文中,IBOutlets的观点和你的主窗口:

@interface MDAppController : NSObject { 
    IBOutlet NSWindow *mainWindow; 
    IBOutlet NSView *firstView; 
    IBOutlet NSView *secondView; 
} 

- (IBAction)changeView:(id)sender; 

@end 

然后在你的实现中,像这样:

#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 

你可以设定,让你想要的按钮用来切换视图每个调用相同changeView:方法,而不是定义为每一个单独的方法。在Interface Builder的nib文件中,您可以设置按钮的标签属性来区分它们。在运行时,当您单击按钮并调用changeView:方法时,该按钮将作为通用sender参数传入,因此您将检查该参数以确定应切换到哪个视图。

+0

所有这些代码在选项卡视图上的优点是什么? –

+0

@PeterHosey:好的,对于一个简单的界面来说,这可能是矫枉过正。不过,作为一名经验丰富的人,我希望您可以想象随着应用程序复杂程度的增加,标签视图方法如何变得不利。例如,我在过去4个月中一直致力于继承的项目。新手原始开发人员(对不起,Bernardo)在单个(2 MB!)笔尖文件中实现了整个应用程序UI。 16个标签查看项目,10个表格视图,100个文本字段,全部加载是否实际使用,导致较长的启动时间并降低性能。将更新我的答案... – NSGod