如何在 Mac OS X 应用程序中从一个 NSViewcontroller 推送到另一个 NSViewcontroller?

Posted

技术标签:

【中文标题】如何在 Mac OS X 应用程序中从一个 NSViewcontroller 推送到另一个 NSViewcontroller?【英文标题】:How to push from one NSViewcontroller to another NSViewcontroller in Mac OS X application? 【发布时间】:2018-05-22 06:23:19 【问题描述】:

我尝试了下面的代码,但没有工作。我是 Mac OS X App 的新手,请帮助我。

NSStoryboard *Story=[NSStoryboard storyboardWithName:@"Main" bundle:nil];

TableViewVC *Controller=[Story instantiateViewControllerWithIdentifier:@"TableViewVC"];

[self presentViewController:Controller animator:YES];

【问题讨论】:

【参考方案1】:

这里有四种方式来呈现 NSViewController

AsPopover

- (IBAction)presentAsPopover:(NSButton *)sender 
    NSViewController* vc = [[MyViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:vc
        asPopoverRelativeToRect:sender.frame
                         ofView:self.view
                  preferredEdge:NSMinYEdge
                       behavior:NSPopoverBehaviorTransient];


表格

- (IBAction)presentAsSheet:(NSButton *)sender 
    NSViewController* vc = [[MyViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewControllerAsSheet:vc];


作为模态

- (IBAction)presentAsModal:(NSButton *)sender 
    NSViewController* vc = [[MyViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewControllerAsModalWindow:vc];


与动画师

- (IBAction)presentWithAnimator:(NSButton *)sender 
    id animator = [[MyCustomAnimator alloc] init];
    NSViewController* vc = [[MyViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:vc animator:animator];


这里是动画师:

@interface MyCustomAnimator : NSObject <NSViewControllerPresentationAnimator>

@end

@implementation MyCustomAnimator

显示

- (void)animatePresentationOfViewController:(NSViewController *)viewController fromViewController:(NSViewController *)fromViewController 

    NSViewController* bottomVC = fromViewController;
    NSViewController* topVC = viewController;

    // make sure the view has a CA layer for smooth animation
    topVC.view.wantsLayer = YES;

    // set redraw policy
    topVC.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;

    // start out invisible
    topVC.view.alphaValue = 0;

    // add view of presented viewcontroller
    [bottomVC.view addSubview:topVC.view];


    // adjust size and colour
    CGRect frame = NSRectToCGRect(bottomVC.view.frame);
    frame = CGRectInset(frame, 40, 40);
    [topVC.view setFrame:NSRectFromCGRect(frame)];
    topVC.view.layer.backgroundColor = [NSColor grayColor].CGColor;

    // Do some CoreAnimation stuff to present view
    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) 
        context.duration = 0.5;
        topVC.view.animator.alphaValue = 0.8;
     completionHandler:nil];


隐藏

- (void)animateDismissalOfViewController:(NSViewController *)viewController fromViewController:(NSViewController *)fromViewController 

    //NSViewController* bottomVC = fromViewController;
    NSViewController* topVC = viewController;

    // make sure the view has a CA layer for smooth animation
    topVC.view.wantsLayer = YES;

    // set redraw policy
    topVC.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;

    // Do some CoreAnimation stuff to present view
    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) 
        context.duration = 0.5;
        topVC.view.animator.alphaValue = 0;
     completionHandler:^
        [topVC.view removeFromSuperview];
    ];



@end

记住

控制 NSViewController 的行为,

NSViewController 的xib 文件存在,很容易控制。

没有xib文件,或者在storyboard中设置,有点复杂

【讨论】:

【参考方案2】:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myVC = storyboard.instantiateViewControllerWithIdentifier("TableViewVC")
self.presentViewController(myVC, animated: true, completion: nil)

您还应该像这样更改您的故事板 ID:

“TableViewVC”将此作为故事板ID

【讨论】:

这是 ios 应用程序的代码工作,但我想要 Mac OS X 应用程序。

以上是关于如何在 Mac OS X 应用程序中从一个 NSViewcontroller 推送到另一个 NSViewcontroller?的主要内容,如果未能解决你的问题,请参考以下文章

如何构建本机 Mac OS X 安装程序(在非 Mac 平台上)?

如何在 NSView (Mac OS X) 上显示徽章

如何在Mac OS X上面指定Eclipse启动时用指定的某一版本JDK

在 MAC OS X 10.8.x 上,AppStore 应用程序如何安装内核扩展?

如何在 Mac OS X 上使用 C++ 播放声音?

如何在Mac OS X上安装 Ruby运行环境