在窗口中切换自定义视图
Posted
技术标签:
【中文标题】在窗口中切换自定义视图【英文标题】:Switching custom views in a window 【发布时间】:2012-06-21 01:57:31 【问题描述】:我有一个 .xib,它有一个带有 NSToolbar
的窗口,并且有多个自定义视图,所有这些都是同一个 xib 的一部分。当我单击工具栏图标时,它会切换视图,但不会显示该视图上的任何按钮或其他对象。
我知道视图正在切换,因为两个视图的大小不同,并且窗口正在调整
切换视图的代码:
NSInteger tag = [sender tag];
NSView *view = [self viewForTag:tag];
NSView *previousView = [self viewForTag: currentViewTag];
currentViewTag = tag;
NSRect newFrame = [self newFrameForNewContentView:view];
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.1];
if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask)
[[NSAnimationContext currentContext] setDuration:1.0];
[[[self.window contentView] animator] replaceSubview:previousView with:view];
[[self.window animator] setFrame:newFrame display:YES];
[NSAnimationContext endGrouping];
【问题讨论】:
【参考方案1】:这可能不是最好的方式,我不确定,但我切换视图的方式是这样的:
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
self.firstSubView = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
self.secondSubView = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self.customView addSubview: self.firstSubView.view];
self.firstSubView.view.frame = self.customView.bounds;
self.firstViewIsSelected = YES;
- (IBAction)viewSwitched:(id)sender
if (self.firstViewIsSelected)
[self.firstSubView.view removeFromSuperview];
[self.customView addSubview: self.secondSubView.view];
self.secondSubView.view.frame = self.customView.bounds;
else
[self.secondSubView.view removeFromSuperview];
[self.customView addSubview: self.firstSubView.view];
self.firstSubView.view.frame = self.customView.bounds;
self.firstViewIsSelected = !self.firstViewIsSelected;
【讨论】:
以上是关于在窗口中切换自定义视图的主要内容,如果未能解决你的问题,请参考以下文章