首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSVIewController之间的导航

NSVIewController之间的导航
EN

Stack Overflow用户
提问于 2013-09-04 08:32:02
回答 1查看 3.5K关注 0票数 4

我是非常新的MAC OSX应用程序开发。在我的应用程序中,我有三个NSViewControllers,它们是PracticeController、NoteController和QuestionController。我必须从PracticeController和QuestionController导航到viewController,然后回到NoteController导航的viewController。

例如:当我们从NoteController从PracticeController导航到back时,当我们从NoteController点击back按钮时,我必须到PracticeController,当我们从QuestionController导航到NoteController时,当我们从NoteController点击back按钮时,我必须到QuestionController。

请帮我怎么做?我正在寻找它。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-03 00:38:43

嗯,经过长时间的搜索,我找到了一个开源库,它将UIKit移植到MacOSX。

https://github.com/BigZaphod/Chameleon.git

但对我来说太复杂了,所以我写了我自己的导航控制器。

NSNavigationController.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#import <Cocoa/Cocoa.h>

@class NSNavigationController;

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

BaseViewController.m

代码语言:javascript
复制
#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。我没有实现视图动画。希望能帮上忙。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18608983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档