Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing code in a different window/view controller from the current window/view controller

Tags:

macos

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.

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.

Thanks for your help!

UPDATE:

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()

     }

 }

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)

     }

 }
like image 939
cobbcobb Avatar asked Jan 15 '15 02:01

cobbcobb


1 Answers

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

let viewController = windowController.window?.contentViewController as YourCustomViewController
// do stuff
viewController.method()
like image 76
bluedome Avatar answered Dec 31 '22 20:12

bluedome