Home > Back-end >  How to call function in NSViewController?
How to call function in NSViewController?

Time:08-12

class CustViewController: NSViewController {
    
    @IBOutlet weak var tableView: NSTableView!
    @IBOutlet weak var statusLabel: NSTextField!
    fileprivate var selectedOptionFromMenu = ""
   
    @objc var contacts:[Person] = []
    @objc var backUpContacts:[Person] = []

    @IBAction func printCustomers(_ sender: Any?) {

I would like to call the printCustomers function from another class (NSWindowController). How is this coded in the NSWindowController class?

I tried the following:

let printAction = CustViewController.printCustomers(<#T##self: CustViewController##CustViewController#> )

but don't know how to code argument in this and this may be not be the way to do this?

CodePudding user response:

I used an observer to notify when to run the code in the ViewController
The following is the code I used to accomplish this:
In the main WindowController
 public let printNotification = Notification.Name("printNotification")
 @IBAction func cashToMePrinting(_ sender: Any?) {
          switch activeWindow {
        case activeView.customerView:
            let printCustNC = NotificationCenter.default
            printCustNC.post(name: printNotification, object: nil)
        default:
            print()
        }
    }
Added the @IBAction to the responder chain using toolbar print item
Then in the ViewController I added:    
 @objc func reactToNotification(_ sender: Notification) {
       // Do what you need, including updating IBOutlets
    printCustomers(Any?.self)
    }
  • Related