macos - NSVIewController 之间的导航

标签 macos nsviewcontroller

我对 MAC OSX 应用程序开发很陌生。
在我的应用程序中,我有三个 NSViewController,它们是 PracticeController、NoteController 和 QuestionController。我必须从 PracticeController 和 QuestionController 导航到 NoteViewController,然后返回到 NoteController 导航的 viewController。

例如:当我们从 PracticeController 导航到 NoteController 时,当我们从 NoteController 点击后退按钮时,我必须来到 PracticeController,当我们从 QuestionController 导航到 NoteController 时,当我们从 NoteController 中点击后退按钮时,我必须来到问题 Controller 。

请帮我如何做到这一点?我正在努力寻找它。谢谢。

最佳答案

好吧,经过长时间的搜索,我找到了一个将 UIKit 移植到 MacOSX 的开源库。
https://github.com/BigZaphod/Chameleon.git
但是对我来说太复杂了,所以我自己写了导航 Controller 。

NSNavigationController.h

#import <Cocoa/Cocoa.h>

@class BaseViewController;
@interface NSNavigationController : NSResponder
@property (nonatomic, strong) BaseViewController *rootViewController;

- (id)initWithRootViewController:(BaseViewController *)rootViewController;
- (NSView*)view;

- (void)pushViewController:(BaseViewController *)viewController animated:(BOOL)animated;
- (BaseViewController *)popViewControllerAnimated:(BOOL)animated;
@end

NSNavigationController.m
#import "NSNavigationController.h"
#import "AppDelegate.h"
#import "BaseViewController.h"

@interface NSNavigationController ()
@property (nonatomic, strong) NSMutableArray *viewControllerStack;
@end

@implementation NSNavigationController
- (id)initWithRootViewController:(BaseViewController *)rootViewController
{
    self = [super init];
    if (self) {
        self.rootViewController = rootViewController;
        self.rootViewController.navigationController = self;
        self.viewControllerStack = [[NSMutableArray alloc] initWithObjects:self.rootViewController, nil];
    }
    return self;
}

- (NSView*)view
{
    BaseViewController *topViewController = [self.viewControllerStack objectAtIndex:[self.viewControllerStack count] - 1];
    return topViewController.view;
}

- (void)pushViewController:(BaseViewController *)viewController animated:(BOOL)animated
{
    if (viewController != nil) {
        [self removeTopView];
        [self.viewControllerStack addObject:viewController];
        viewController.navigationController = self;
        [self addTopView];
    }
}

- (BaseViewController *)popViewControllerAnimated:(BOOL)animated
{
    BaseViewController *topViewController = [self.viewControllerStack objectAtIndex:[self.viewControllerStack count] - 1];
    [self removeTopView];
    [self.viewControllerStack removeLastObject];
    [self addTopView];

    return topViewController;
}

- (void)removeTopView
{
    BaseViewController *topViewController = [self.viewControllerStack objectAtIndex:[self.viewControllerStack count] - 1];
    [topViewController.view removeFromSuperview];
}

- (void)addTopView
{
    BaseViewController *topViewController = [self.viewControllerStack objectAtIndex:[self.viewControllerStack count] - 1];
    AppDelegate *delegate = (AppDelegate*)[NSApp delegate];
    [delegate.window.contentView addSubview:topViewController.view];
}
@end

基 View Controller .h
#import <Cocoa/Cocoa.h>

@class NSNavigationController;

@interface BaseViewController : NSViewController
@property (nonatomic, weak) NSNavigationController *navigationController;
@end

基 View Controller .m
#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
    }
    return self;
}

@end

它是最简单的 NavigationController。我没有实现 View 动画。希望它能有所帮助。

关于macos - NSVIewController 之间的导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18608983/

相关文章:

objective-c - NSTextView 无法使用自动布局正确调整大小

c# - 在 iOS 上将自定义呈现的页面移动到状态栏下方

macos - 为 Macintosh 开发 Word 插件

objective-c - NSString drawAtPoint 文本未显示

macos - 如何通过按 ESC 键关闭窗口(NSWindowController)?

swift - 如何让我的 NSViewController 进入另一个 ViewController 而不离开窗口?

swift 4 : Issues passing data from one NSViewController to another

swift - 强制 NSSearchField 在成为第一响应者时开始编辑

c# - 我想将我的 Iphone 连接到 MAC mini,并使用 Xamarin UI 测试从我的 Windows 计算机远程运行测试

找不到要在 c 中读取的 .txt 文件