【问题标题】:os x equivalent to ios's rootViewControlleros x 相当于 ios 的 rootViewController
【发布时间】:2013-08-05 19:08:23
【问题描述】:

什么是 ios'(Ruby 风格)的 OS X 等价物:

@window.rootViewController = NSViewController.alloc.initWithNibName(nil, bundle: nil)

有两个方面:

  • 处理 os x 中缺少 rootViewController
  • 执行与 initWithNibName(nil, bundle: nil) 等效的操作,但在 os x 上失败

我正在尝试在代码中构建一个窗口(没有 nib)...并遵循 Pragmatic 的 RubyMotion 程序员指南(为 iOS 编写)。

【问题讨论】:

    标签: cocoa-touch cocoa rubymotion macruby


    【解决方案1】:

    在我的例子中,我有一个由窗口拥有的视图控制器,所以下面的代码对我有用:

    let viewController = NSApplication.sharedApplication().keyWindow!.contentViewController as! MyViewController
    

    MyViewController 是我的 NSViewController 子类。

    【讨论】:

    • 尽管有多个 viewControllers 这对我有用 - 我只是检查了 contentViewController 在获取我需要的东西之前能够向下转换到正确的子类
    【解决方案2】:

    OS X 应用程序中没有“rootViewController”的概念。这是因为应用程序由一个或多个窗口和/或菜单栏组成,它们都不是“根”。

    但是,您可以从视图控制器开始找到窗口控制器:

    [[[self view] window] delegate];
    

    如果您希望在窗口控制器上调用自定义方法,最好create a delegate 这样对控制器的假设不会给您带来麻烦。

    NSViewController.alloc.initWithNibName(nil, bundle: nil) 的等价物是:

    [[NSViewController alloc] initWithNibName:nil bundle:nil];

    方法调用嵌套在方括号中,带有多个参数的方法只是label1:parameter1 label2:parameter2...

    不过,通常情况下,您会拥有一个自定义的 NSViewController 子类,您可以将其实例化。

    【讨论】:

    • 感谢 rootViewController 信息。你知道 initWithNibName(nil, bundle: nil) 的等价物是什么吗?
    • Doh,我忽略了问题的那一部分,我已将答案添加到我的答案中。
    【解决方案3】:

    如果您想像在 iOS 中一样重置 rootViewController,您可以使用以下代码:

    let storyBoard = NSStoryboard(name: NSStoryboard.Name(rawValue:"Main"), bundle: Bundle.main)
    let homeViewController = storyBoard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "SIHome")) as! HomeViewController
    NSApp.keyWindow?.contentViewController = homeViewController
    
    • NSAppNSApplication.shared() 的快捷方式
    • rootViewController 在 OSX 中称为 contentViewController

    【讨论】:

      猜你喜欢
      • 2014-01-04
      • 1970-01-01
      • 2011-06-02
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 2011-03-01
      相关资源
      最近更新 更多