在与当前窗口/视图控制器不同的窗口/视图控制器中执行代码 [英] Executing code in a different window/view controller from the current window/view controller

查看:44
本文介绍了在与当前窗口/视图控制器不同的窗口/视图控制器中执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的Xcode 6中将此代码用于我使用Apple Swift为OSx构建的项目.我没有在这里引用的通常的.m和.h文件.这是一个带有AppDelegate.swift的故事板,每个窗口都有一个类文件.

I'm using this code below in Xcode 6 for a project I'm building for OSx using apple swift. I don't have the usual .m and .h files that I see referenced on here. It's a storyboard with an AppDelegate.swift and a class file for each window.

下面的代码从OS X应用程序的主窗口中打开一个附加窗口以显示视频.如何存储对新窗口对象的引用,并使用该引用从当前窗口在新窗口内执行功能?我正在尝试在原始视图控制器/窗口中创建播放控件,以控制新窗口中包含的视频.我知道不同的窗口是不同类的不同实例,但是我仍在尝试掌握它们如何实时相互交谈,而不是我看到的大多数示例,其中一个视图控制器将信息传递给另一个视图控制器包含在同一窗口中.

The code below opens an additional window from a primary window in an OS X app to display a video. How can I store a reference to the new window object and use that to execute functions inside the new window from my current window? I'm trying to create playback controls in the original view controller/window to control a video contained in the new window. I understand that different windows are different instances of different classes, but I'm still trying to grasp how they can speak to each other in real time rather than most of the examples I've seen where one view controller passes information to another view controller contained in the same window.

感谢您的帮助!

更新:

建议的答案在最初打开窗口的功能内起作用.但是,我无法在不同的函数中访问该方法.我试图在函数范围之外声明变量,但无法访问其他ViewController中的方法

The answer suggested works inside the function where the window is originally opened. However, I cannot access the method in a different function. I tried declaring the variable outside the scope of the function, but I'm not able to access the methods in the other ViewController

 class ViewController: NSViewController {

     var MainVideoControllerWindow: NSWindowController?
     var MainVideoController: NSViewController?

     @IBAction func openPanel(sender: AnyObject) {

         if (MainVideoControllerWindow == nil) {

             //THIS SECTION WORKS PERFECTLY
             let storyboard = NSStoryboard(name: "Main", bundle: nil)
             MainVideoControllerWindow = storyboard?.instantiateControllerWithIdentifier("MainVideo") as? NSWindowController
             MainVideoControllerWindow!.showWindow(sender)

             //THIS LINE WORKS AS SUGGESTED
             let MainVideoController = MainVideoControllerWindow!.window?.contentViewController as MainVideo

             //I CAN ACCESS THIS METHOD CONTAINED IN OTHER VIEW CONTROLLER
             MainVideoController.playVideo(sender)

             //TRYING THIS INSTEAD TO ALLOW ACCESS TO OTHER FUNCTIONS WITHIN THIS CLASS
             MainVideoController = MainVideoControllerWindow!.window?.contentViewController as MainVideo

             //THIS LINE RESULTS IN AN ERROR (NSViewController? does not have a member named playVideo)
             MainVideoController.playVideo(sender)

             //I TRIED UNWRAPPING IT AND STILL THE SAME ERROR
             MainVideoController!.playVideo(sender)

         } else {
             MainVideoControllerWindow!.showWindow(sender)
         }

     }

     @IBAction func btnPlayVideo(sender: AnyObject) {

         //CANNOT ACCESS THE METHOD HERE
         MainVideoController.playVideo()

     }

 }

最终更新:这是代码的最后工作块,它使我可以从当前窗口内的任何函数访问新Window的函数.我更改了MainVideoController var声明的类型以匹配我的自定义类,并添加了条件"as".将contentViewController捕获到变量中时.谢谢@bluedome的帮助!

FINAL UPDATE: This is the final working block of code that allowed me access to the new Window's functions from any function within the current window. I changed the type of the MainVideoController var declaration to match my custom class and added the conditional "as?" when capturing the contentViewController into the variable. Thank you @bluedome for your help!

 class ViewController: NSViewController {

     var MainVideoControllerWindow: NSWindowController?
     var MainVideoController: MainVideo?

     @IBAction func openPanel(sender: AnyObject) {

         if (MainVideoControllerWindow == nil) {

             //OPEN NEW WINDOW
             let storyboard = NSStoryboard(name: "Main", bundle: nil)
             MainVideoControllerWindow = storyboard?.instantiateControllerWithIdentifier("MainVideo") as? NSWindowController
             MainVideoControllerWindow!.showWindow(sender)

             //SAVE REFERENCE TO VIEW CONTROLLER IN NEW WINDOW
             MainVideoController = MainVideoControllerWindow!.window?.contentViewController as? MainVideo

         } else {
             MainVideoControllerWindow!.showWindow(sender)
         }

     }

     @IBAction func btnPlayVideo(sender: AnyObject) {

         //CAN ACCESS THE METHOD IN OTHER WINDOW'S VIEW CONTROLLER HERE
         MainVideoController!.playVideo(sender)

     }

 }

推荐答案

您可以像这样从 NSWindow contentViewController 获取 NSViewController .

You can get NSViewController from NSWindow's contentViewController like this.

let viewController = windowController.window?.contentViewController as YourCustomViewController
// do stuff
viewController.method()

这篇关于在与当前窗口/视图控制器不同的窗口/视图控制器中执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆