Hi
I created a new project for macOS I added methods to the default View Controller and it shows fine in the list that apears when control draging
from a menu item to the responder, my question is how to link a menu item to a method in the second window View Controller ? cause they
dont apear in the list when Control Draging from menu item to reponder ?
Kindest Regards
Well, I did it this way.
Create a storyboard project.
By default, got ViewController.
- Create an IBAction in it
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
@IBAction func testForMainVCMenu(_ sender: NSMenuItem) {
print("test For Main VC")
}
}
- Create a new file, Cocoa clas, subclass of NSViewController, named SecondViewController
- Create an IBAction inside:
import Cocoa
class SecondViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
@IBAction func testForSecondVCMenu(_ sender: NSMenuItem) {
print("test For Second VC")
}
}
- Create in storyboard a new WindowController: 2 views on screen: windowController and ViewController
- Set class of windowController to SecondViewController THIS IS MANDATORY
- Add 2 menu items inFile Menu : first VC Menu and second Menu VC
- Control drag from each menu item to First Responder of Application (above the menu bar), and select the appropriate IBAction.
- created a button in ViewController to segue to SecondWindow
When running, note that the menu item is activated only when the window that contains the action is FirstResponder.