Mac App 窗口切换

特性

  1. 新建一个新的window窗口,与之前的创建没有任何关联
  2. 可以自定义新窗口大小,位置等样式
  3. 旧窗口可以关闭,也可以不关闭

代码

核心代码

#import <Cocoa/Cocoa.h>

@interface BaseWindowController : NSWindowController
@property (nonatomic, weak) NSViewController *rootViewController;
- (instancetype)initWithRootViewController:(NSViewController *)rootViewController;
@end
#import "BaseWindowController.h"

@interface BaseWindowController ()
@end

@implementation BaseWindowController

- (instancetype)initWithRootViewController:(NSViewController *)rootViewController
{
    self = [super init];
    if (self) {
        self.window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 400) styleMask:(NSWindowStyleMaskTitled) backing:(NSBackingStoreBuffered) defer:YES];
        self.window.windowController = self;
        self.window.movableByWindowBackground = YES;
        self.rootViewController = rootViewController;
        self.contentViewController = self.rootViewController;
    }
    return self;
}
@end

窗口

#import "BaseWindowController.h"

@interface HomeWindowController : BaseWindowController

@end
#import "HomeWindowController.h"

@interface HomeWindowController ()

@end

@implementation HomeWindowController

- (void)windowDidLoad {
    [super windowDidLoad];
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

@end

控制器

#import "BaseViewController.h"

@interface HomeViewController : NSViewController
@end
#import "HomeViewController.h"

@implementation HomeViewController
@end

弹出代码

- (IBAction)openNewWindoAction:(NSButton *)sender
{
    HomeViewController *vc = [[HomeViewController alloc] init];
    HomeWindowController *newWindow = [[HomeWindowController alloc] initWithRootViewController:vc];
    [newWindow showWindow:self];
}

报错

2018-11-12 15:31:40.833165+0800 CodeM[7339:233677] [General] -[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: HomeViewController in bundle (null).
2018-11-12 15:31:40.836634+0800 CodeM[7339:233677] [General] (
    0   CoreFoundation                      0x00007fff4aa3b2db __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff71bf0c76 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff4aaccd7d +[NSException raise:format:] + 205
    3   AppKit                              0x00007fff4801b24c -[NSNib _initWithNibNamed:bundle:options:] + 634
    4   AppKit                              0x00007fff4801af82 -[NSViewController _nibWithName:bundle:] + 182
    5   AppKit                              0x00007fff4801ab84 -[NSViewController loadView] + 139
    6   AppKit                              0x00007fff47f92a9e -[NSViewController _loadViewIfRequired] + 75
    7   AppKit                              0x00007fff47f92a09 -[NSViewController view] + 30
    8   AppKit                              0x00007fff48111365 -[NSWindow _contentViewControllerChanged] + 109
    9   AppKit                              0x00007fff48a8e677 -[NSWindowController setContentViewController:] + 135
    10  CodeM                               0x0000000100002c7a -[BaseWindowController initWithRootViewController:] + 650
    11  CodeM                               0x0000000100005819 -[HomeViewController createFileAction:] + 121
    12  AppKit                              0x00007fff486e8a43 -[NSApplication(NSResponder) sendAction:to:from:] + 312
    13  AppKit                              0x00007fff4818e53f -[NSControl sendAction:to:] + 86
    14  AppKit                              0x00007fff4818e467 __26-[NSCell _sendActionFrom:]_block_invoke + 136
    15  AppKit                              0x00007fff4818e36d -[NSCell _sendActionFrom:] + 183
    16  AppKit                              0x00007fff481cf688 -[NSButtonCell _sendActionFrom:] + 97
    17  AppKit                              0x00007fff4818cbd6 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2438
    18  AppKit                              0x00007fff481cf3cf -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 777
    19  AppKit                              0x00007fff4818b670 -[NSControl mouseDown:] + 965
    20  AppKit                              0x00007fff48887d6d -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 5891
    21  AppKit                              0x00007fff488849c4 -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2359
    22  AppKit                              0x00007fff48883c70 -[NSWindow(NSEventRouting) sendEvent:] + 497
    23  AppKit                              0x00007fff486e5236 -[NSApplication(NSEvent) sendEvent:] + 2462
    24  AppKit                              0x00007fff47f458b5 -[NSApplication run] + 812
    25  AppKit                              0x00007fff47f14a72 NSApplicationMain + 804
    26  CodeM                               0x0000000100007a42 main + 34
    27  libdyld.dylib                       0x00007fff7280a015 start + 1
    28  ???                                 0x0000000000000003 0x0 + 3
)

分析

2018-11-12 15:31:40.833165+0800 CodeM[7339:233677] [General] -[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: HomeViewController in bundle (null).

根据这个报错信息,意思就是说 HomeViewController 这个类的实例没有获取到,而且是是<u>使用了NSNib初始化方法加载</u>,这就意味着有两种解决方案

解决

方法一

在创建HomeViewController类的时候,勾选xib选项,即使用xib方式创建

方法二

- (instancetype)initWithNibName:(NSNibName)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self.view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 400, 400)];
    return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}

上面提到过,旧的窗口可以关闭

- (IBAction)openNewWindoAction:(NSButton *)sender
{
    HomeViewController *vc = [[HomeViewController alloc] init];
    HomeWindowController *newWindow = [[HomeWindowController alloc] initWithRootViewController:vc];
    [self.view.window close];
    [newWindow showWindow:self];
}