Jun*_*uno 3 macos nsviewcontroller
我是MAC OSX应用程序开发的新手.在我的应用程序中,我有三个NSViewControllers,它们是PracticeController,NoteController和QuestionController.我必须从PracticeController和QuestionController导航到NoteViewController并返回到NoteController导航的viewController.
例如:当我们从PracticeController导航到NoteController时,当我们点击NoteController的后退按钮时,我必须来到PracticeController,当我们从QuestionController导航到NoteController时,当我们点击NoteController的后退按钮时,我必须来QuestionController.
请帮帮我怎么做?我正在寻找它.谢谢.
好吧,经过长时间的搜索,我找到了一个开源库,将UIKit移植到MacOSX.
https://github.com/BigZaphod/Chameleon.git
但这对我来说太复杂了,所以我编写了自己的导航控制器.
#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
Run Code Online (Sandbox Code Playgroud)
#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
Run Code Online (Sandbox Code Playgroud)
#import <Cocoa/Cocoa.h>
@class NSNavigationController;
@interface BaseViewController : NSViewController
@property (nonatomic, weak) NSNavigationController *navigationController;
@end
Run Code Online (Sandbox Code Playgroud)
#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
Run Code Online (Sandbox Code Playgroud)
这是最简单的NavigationController.我没有实现视图动画.希望它可以提供帮助.