首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Mac应用程序中从一个NSViewcontroller推送到另一个NSViewcontroller?

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

Stack Overflow用户
提问于 2018-05-22 14:23:20
回答 2查看 545关注 0票数 0

我尝试了下面的代码,但不起作用。我是Mac OS X应用程序的新手,请帮助我。

代码语言:javascript
复制
NSStoryboard *Story=[NSStoryboard storyboardWithName:@"Main" bundle:nil];

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

[self presentViewController:Controller animator:YES];
EN

回答 2

Stack Overflow用户

发布于 2020-04-02 15:35:32

下面是呈现NSViewController的四种方式

  • AsPopover
  • (IBAction)presentAsPopover:(NSButton *)发送方{ NSViewController* vc = [MyViewController alloc :nil bundle:nil];自呈现视图控制器:vc asPopoverRelativeToRect:sender.frame ofView:self.view首选边缘:NSMinYEdge行为:NSPopoverBehaviorTransient瞬态;}
  • AsSheet
  • (IBAction)presentAsSheet:(NSButton *)发送方{ NSViewController* vc = [MyViewController alloc :nil bundle:nil];自呈现presentViewControllerAsSheet: vc;}
  • WithAnimator
  • (IBAction)presentWithAnimator:(NSButton *)发送方{ NSViewController* vc= [MyViewController alloc :nil bundle:nil];自呈现presentViewControllerAsModalWindow:vc;}
  • WithAnimator
  • (IBAction)presentWithAnimator:(NSButton*)发送方{ id animator =[MyViewController alloc ];NSViewController* vc = [MyViewController alloc :nil bundle:nil];self presentViewController:vc animator:animator;}

这是动画制作:

代码语言:javascript
复制
@interface MyCustomAnimator : NSObject <NSViewControllerPresentationAnimator>

@end

@implementation MyCustomAnimator

  • to Show
  • (void)animatePresentationOfViewController:(NSViewController *)viewController fromViewController:( NSViewController * )fromViewController { NSViewController* bottomVC = fromViewController;NSViewController* topVC = viewController;//确保视图有用于平滑动画的CA层topVC.view.wantsLayer = YES;//设置重绘策略topVC.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;//开始看不见的topVC.view.alphaValue = 0;//添加呈现的viewcontroller bottomVC.view addSubview:topVC.view的视图;//调整大小和颜色CGRect NSRectToCGRect= NSRectToCGRect(bottomVC.view.frame);frame = CGRectInset( frame,40,40);topVC.view setFrame:NSRectFromCGRect(frame);topVC.view.layer.backgroundColor = NSColor grayColor.CGColor;//做一些CoreAnimation东西来呈现视图NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) { context.duration = 0.5;topVC.view.animator.alphaValue = 0.8;} completionHandler:nil;}
  • to Hide
  • (void)animateDismissalOfViewController:(NSViewController *)viewController fromViewController:( NSViewController * )fromViewController { //NSViewController* bottomVC = fromViewController;NSViewController* topVC = viewController;//确保视图有用于平滑动画的CA层topVC.view.wantsLayer = YES;//设置重绘策略topVC.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;//执行一些上下文操作来呈现视图[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) { context.duration = 0.5;topVC.view.animator.alphaValue = 0;} completionHandler:^{ topVC.view removeFromSuperview;}];} @end

请牢记

为了控制NSViewController的行为,

有了xib文件,就很容易控制NSViewController。

在没有xib文件或在storyboard中设置的情况下做这件事有点复杂

票数 2
EN

Stack Overflow用户

发布于 2018-05-22 18:49:02

代码语言:javascript
复制
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myVC = storyboard.instantiateViewControllerWithIdentifier("TableViewVC")
self.presentViewController(myVC, animated: true, completion: nil)

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

"TableViewVC“将其作为情节提要id

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

https://stackoverflow.com/questions/50461072

复制
相关文章

相似问题

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