Mac App 窗口切換

特性

  1. 新建一個(gè)新的window窗口,與之前的創(chuàng)建沒有任何關(guān)聯(lián)
  2. 可以自定義新窗口大小,位置等樣式
  3. 舊窗口可以關(guān)閉,也可以不關(guān)閉

代碼

核心代碼

#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];
}

報(bào)錯(cuò)

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).

根據(jù)這個(gè)報(bào)錯(cuò)信息,意思就是說 HomeViewController 這個(gè)類的實(shí)例沒有獲取到,而且是是<u>使用了NSNib初始化方法加載</u>,這就意味著有兩種解決方案

解決

方法一

在創(chuàng)建HomeViewController類的時(shí)候,勾選xib選項(xiàng),即使用xib方式創(chuàng)建

方法二

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

上面提到過,舊的窗口可以關(guān)閉

- (IBAction)openNewWindoAction:(NSButton *)sender
{
    HomeViewController *vc = [[HomeViewController alloc] init];
    HomeWindowController *newWindow = [[HomeWindowController alloc] initWithRootViewController:vc];
    [self.view.window close];
    [newWindow showWindow:self];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容