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 in the CustViewController 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?
I thought of it after posting the answer.
In fact, IBAction is an instance func, not a class one.
So you need to call on an instance.
At least 2 ways:
- keep a reference of the CustViewController controller(myController), and just call
let printAction = myController.printCustomers(self)
- create an instance on which to call
let printAction = CustViewController().printCustomers(self)
it will compile but this may not work as expected if IBAction uses some CustViewController properties
Other patterns are:
- use delegation (that may be the cleanest way)
- send notification for the second VC to CustViewController
Hope that helps better.