【问题标题】:Opening window + view from an other view controller从另一个视图控制器打开窗口 + 视图
【发布时间】:2018-08-13 02:53:30
【问题描述】:

我有一个 ViewControllerOne。 ViewControllerOne 通过 Ctrl-Drag(在故事板中)连接到菜单按钮 mBtn(这意味着我不知道它是如何以编程方式实现的)。

单击此 mBtn,会出现一个 ViewOne(当前模式)。此 ViewOne 绑定到 ViewControllerOne。 ViewOne 有一个按钮 btnOne。

单击 btnOne 我希望关闭 ViewOne 并显示 ViewTwo。 ViewTwo 属于 ViewControllerTwo 和 WindowControllerTwo。

WindowControllerTwo-ViewControllerTwo 绑定是在新项目上创建的标准情况。

我在 ViewControllerOne 中按钮 btnOne 的 IBAction 中有以下代码:

@IBAction func onbtnOnePressed(sender: AnyObject){

        let m_WindowControllerTwo = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil).instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("WindowControllerTwo")) as! NSWindowController // I have no custom class for the window controller as I don't really know what I can use it for ... 

        let m_ViewTwo = WindowControllerTwo.contentViewController as! ViewControllerTwo // my custom class for ViewTwo

        m_ViewTwo.attributeIWantToPassToThisView = self.x // an attribute I want to pass from view a to view b

        m_WindowControllerTwo.contentViewController = m_ViewTwo // passing the attribute from a to b

        m_WindowControllerTwo.showWindow(self) // this does not work

        self.dismiss(nil)  // see NOTE
    }

此代码实际上不起作用。在逐步调试它时,我看到窗口/视图闪烁但没有出现......

注意:我可以通过 ctrl-drag 将按钮 btnOne 连接到 ViewControllerTwo。这行得通。但是当前的 ViewOne 并没有被解雇!

问题:我在这里做错了什么?在 iOS swift 中,这也有效。我不太了解 WindowController 的东西,所以我需要你的建议。

【问题讨论】:

  • 由于您提到的命名,您的问题非常令人困惑。而不是这样做使用 viewControllers 本身的名称。
  • 改了……我猜现在更清楚了

标签: swift macos view window viewcontroller


【解决方案1】:

而不是这个:m_WindowControllerTwo.showWindow(self)

使用:

let application = NSApplication.shared()
application.runModal(for: wordCountWindow) //this will present WindowControllerTwo modally.

然后要关闭您当前的控制器,请添加以下行:PresentWindowControllerName.close()

【讨论】:

  • 我猜你没有理解我的问题。在您的示例中,您在哪里打开一个新窗口?此外,'dismiss(animated: true, completion: nil)' 是 iOS-swift 而我使用的是 macOS-swift...
  • 很好,帮助很大!你能想出一种将值从第一个视图控制器传递给第二个视图控制器(WindowControllerTwo)的方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多