【问题标题】:How to push from one NSViewcontroller to another NSViewcontroller in Mac OS X application?如何在 Mac OS X 应用程序中从一个 NSViewcontroller 推送到另一个 NSViewcontroller?
【发布时间】: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];

【问题讨论】:

    标签: objective-c macos pushviewcontroller


    【解决方案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 应用程序。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 2018-12-11
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多