Accessing NSViewController Object From AppDelegate

Hello,

 

I have a storyboard with wired NSViewController and NSWindowController outlets.  I want to access view controller’s wired objects from AppDelegate.

 

class AppDelegate: NSObject, NSApplicationDelegate {
  @IBAction func fileOpenClicked(sender:AnyObject) {
       let homeStoryboard = NSStoryboard(name: "Home", bundle: nil)
       let homeWindowController = homeStoryboard.instantiateInitialController() as? NSWindowController
       if let homeWindow = homeWindowController?.window {
            print(homeViewController.label.stringValue) // It works.
            print(homeViewController.inputField.stringValue) // Actually, it returns nothing.
       }
  }
  func applicationWillFinishLaunching(_ notification: Notification) {
  }
}

 

And I am able to get the text string of a label there like shown above.  But I am not able to set a text string to a text field there like the following.

 

let homeStoryboard = NSStoryboard(name: "Home", bundle: nil)
let homeWindowController = homeStoryboard.instantiateInitialController() as? NSWindowController
if let homeWindow = homeWindowController?.window {
  let homeViewController: HomeViewController = (homeWindow.contentViewController)! as! HomeViewController
    homeViewController.inputField.stringValue = "Jim is great."
}

 

I usually use only AppDelegate in developing desktop applications.  So I rarely use NSViewController and NSWindowController.  Anyway, what am I doing wrong?

 

Thanks.

Powered by WPeMatico

About

You may also like...

Comments are closed.