代码之家  ›  专栏  ›  技术社区  ›  David

从其他视图控制器打开窗口+视图

  •  0
  • David  · 技术社区  · 7 年前

    我有一个视图控制器。ViewControllerOne通过Ctrl-Drag(在情节提要中)连接到菜单按钮mBtn(这意味着我不知道它是如何以编程方式实现的)。

    单击此mBtn,将显示ViewOne(显示模式)。此ViewOne已绑定到ViewControllerOne。ViewOne有一个按钮BTONE。

    单击btnOne,我希望取消查看一个,显示查看二个。ViewTwo属于ViewControllerTwo和WindowControllerTwo。

    WindowControllerTwo ViewControllerTwo绑定是在新项目上创建的标准案例。

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

    @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
        }
    

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

    注意:我可以将按钮btnOne与ctrl拖动连接到ViewControllerTwo。这是可行的。但是,当前的ViewOne不会被解雇!

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   Yasir Ghafar    7 年前

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

    使用:

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

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