问题描述
我对MAC OSX应用程序开发非常陌生.在我的应用程序中,我有三个NSViewController,分别是PracticeController,NoteController和QuestionController.我必须从PracticeController和QuestionController导航到NoteViewController,然后返回到NoteController从中导航的viewController.
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.
例如:当我们从PracticeController导航到NoteController时,当我们从NoteController中点击后退按钮时,我必须来到PracticeController,而当我们从QuestionController导航到NoteController时,则当我们从NoteController中点击后退按钮时,我有来到QuestionController.
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.
推荐答案
好吧,经过长时间的搜索,我发现了一个开放源代码库,可将UIKit移植到MacOSX.
https://github.com/BigZaphod/Chameleon.git
但这对我来说太复杂了,所以我写了自己的导航控制器.
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.
#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
这是最简单的NavigationController.我没有实现视图动画.希望能对您有所帮助.
It's the simplest NavigationController. I didn't implement the view animation. Hope it can help.
这篇关于NSVIewController之间的导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!