Mac 开发之导航控制器、窗口视图切换

缘由:Mac 开发控件是继承自 AppKit (相当古老,个人觉得相较于iOS端极其不方便)
iOS 开发控件是继承自 UIKit
AppKit 中并没有导航控制器 :UINavigationController、UITabBarController
窗口视图切换极其不方便,代码也会比较繁杂(iOS中是叫视图切换)。
所以,试写出一个比较方便,合理的窗口视图切换,导航的框架 。MacNavigationController

实例Mac导航栏

一般应用程序都是首选项窗口的创建。需创建一个NSWindowController,设置工具栏,使用一系列自定义视图对象创建一个nib文件。
但是,会需要设置工具栏和视图切换就需要大量代码。
并且每个应用程序基本相同上都是相同,视图切换,和工具栏形式。
所以,有必要创建一个导航控制器、窗口视图切换的公用框架,使用。

MacNavigationController 框架介绍:
MacNavigation 文件夹 -- 导航栏视图切换
SetUpWindowController :设计为每个使用它的应用程序的子类。应将子类设置为名为“Preferences”的nib文件的文件所有者。子类应是NSView实例,这些实例是连接到nib文件中视图的IBOutlet。
AppVCsWindowController:应用程序,需要添加至导航栏的视图,集合。

使用 与 介绍

1.导入 SetUpWindowController、AppVCsWindowController
2.AppDelegate.m中导入并初设导航栏


    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    
    _mainWindowController = [AppVCsWindowController sharedPrefsWindowController];
    _mainWindowController.toolbarDisplayMode = 1;//1、图标+文字 2、文字 3、图标
    [_mainWindowController showWindow:nil];
    
}
 

2.1 去掉Main.storyboard 创建项目默认 中调用 NSWindowController


去掉默认连接跳转线

3.所有 需要导航切换的NSView 的 xib 视图 集成在 Preferences.xib 中
4.Preferences.xib 中 NSView 连线关联 AppVCsWindowController
5.AppVCsWindowController.m 中进行添加需要导航栏切换视图并设置导航栏文字和图标

- (void)setupToolbar{
    
    [self addView:self.generalPreferenceView label:@"General" imageUrl:@"General"];
    [self addView:self.colorsPreferenceView label:@"Colors" imageUrl:@"Colors"];
    [self addView:self.playbackPreferenceView label:@"Playback" imageUrl:@"Playback"];
    [self addView:self.updatesPreferenceView label:@"Updates" imageUrl:@"Updates"];
    [self addFlexibleSpacer];
    [self addView:self.advancedPreferenceView label:@"Advanced" imageUrl:@"Advanced"];
    
    // Optional configuration settings.
    [self setCrossFade:[[NSUserDefaults standardUserDefaults] boolForKey:@"fade"]];
    [self setShiftSlowsAnimation:[[NSUserDefaults standardUserDefaults] boolForKey:@"shiftSlowsAnimation"]];
}

SetUpWindowController.h 介绍

此类方法返回SetUpWindowController类的共享实例。

+ (SetUpWindowController *)sharedPrefsWindowController;

如果要使用名为Preferences之外的其他nib文件,请重写此类。

+ (NSString *)nibName;

通过调用-addView:label:imgUrl: 添加toolbarViews 和设置toolbar 文字、图标 。

- (void)addView:(NSView *)view label:(NSString *)label imageUrl:(NSString *)imageUrl;

调用此方法以显示首选项窗口。例如:[[AppVcWindowController sharedPrefsWindowController] showWindow:nil];

- (IBAction)showWindow:(id)sender

调用此方法可在切换视图时启用或禁用交叉渐变效果。默认值为YES。

- (void)setCrossFade:(BOOL)fade

调用此方法可以在切换视图时启用或禁用Shift键以减慢动画速度。默认值为YES。

- (void) setShiftSlowsAnimation:(BOOL)slow

MacWindowViewJump 文件夹 -- NSWindow 窗口弹出,切换 示例。

其实很简单,如有不懂属性,不明逻辑,请参阅demo。

下一页 (NSWindowController 2 --> NSWindowController 3)

    _nextWindowController = [[NSWindow3Controller alloc] initWithWindowNibName:NSStringFromClass([NSWindow3Controller class])];
    
    //让显示的位置居于屏幕的中心
    [[_nextWindowController window] center];
    
    //显示需要跳转的窗口
    [_nextWindowController.window orderFront:nil];
    
    //关闭当前窗口
    [self.window orderOut:nil];

上一页 (NSWindowController 2 --> NSWindowController 1)

    if ([_lastWindowController isKindOfClass:[NSWindow1Controller class]])
    {
        NSWindow1Controller *mainWC = (NSWindow1Controller *)_lastWindowController;
        
        [self.window close];
        mainWC.nextWindowController = nil;
        
        [mainWC.window orderFront:nil];
        
    }

回首页 (NSWindowController 3 --> NSWindowController 1)

    AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
    
    [self.window close];
    
    [appDelegate.mainWindowController.window makeKeyAndOrderFront:nil];

MacNavigationController demo 代码实例

你可能感兴趣的:(Mac 开发之导航控制器、窗口视图切换)