Navigation between NSVIewController

主宰稳场 提交于 2019-12-05 02:32:49

问题


I am very new to MAC OSX app development. In my application I have three NSViewControllers, which are PracticeController, NoteController and QuestionController. I have to navigate to NoteViewController from PracticeController and QuestionController and comeback to the viewController from which NoteController has navigated.

For example: when we navigate to NoteController from PracticeController, then when we tap on back button from NoteController I have to come to PracticeController, and when we navigate to NoteController from QuestionController, then when we tap on back button from NoteController I have to come to QuestionController.

Please help me how to do this? I am badly searching for it. Thanks.


回答1:


well, after a long time search, I found a open source library which ports the UIKit to MacOSX.
https://github.com/BigZaphod/Chameleon.git
But it's too complicated for me, so I wrote my own Navigation 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

BaseViewController.h

#import <Cocoa/Cocoa.h>

@class NSNavigationController;

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

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

It's the simplest NavigationController. I didn't implement the view animation. Hope it can help.



来源:https://stackoverflow.com/questions/18608983/navigation-between-nsviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!