0

我有一个 .xib,它有 1 个窗口,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];
4

1 回答 1

0

这可能不是最好的方法,我不确定,但我切换视图的方式是这样的:

#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;
}
于 2013-02-20T13:22:24.173 回答