Menu Linking to A Non Default View Controller Method

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

Answered by Claude31 in 393775022

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.

Could you explain more precisely the setup ?


What is the "default view controller" ? Is it AppDelegate.swift ?

Or did you create the project with a storyboard ?

I created the project with StoryBoard so the default is ViewController.Swift

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


Which responder is it ?

Is it the First Responder in Application ?

Can you show the ViewController complete code ?

The way I know it and working with me is draging from menu item to application first responder, below is link to the simple project,

so you can see Im trying to link a menu item to a function in myVC.Swift so the menu item will be active only when Window 2 is active


https://www.dropbox.com/s/5pnbe020mpuvxs0/Menus%20Demo.zip?dl=0

I tried to send you a DropBox link for a compressed project its being moderated though, not sure whats the policy here for posting

links, files and images

To send a link, type a space in the URL

like.

h ttps://someURL

Seems its nto working how many spaces I add it still take it as a hyper link

I tried with an existing link to check if moderated:


h ttps://www.dropbox.com/s/5sfpat05m7un2pg/Screenshot%202019-11-03%20at%209.56.56%20PM.png?dl=0


You should first copy the link into a text editor, add the space and then copy the whole in the post.

Otherwise it is still considered as link.


Otherwise, if you drop me an email address, we shall exchange through mails.

Accepted Answer

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.

Thank you very much your code did the trick, I made the same but in second VC I didnt add IBAction so it didnt show

in the First Responder list, but surprisingly it works if it have same name of first VC IBAction but menu item then will

be active for both windows. how to get your mail though ?


Kindest Regards

To get my email, send me yours first (I will rapidly remove the message).


And don't forget to mark the thread as closed by marking the correct answer.

sure my mail is ammar.s@me.com, thanks allot

Menu Linking to A Non Default View Controller Method
 
 
Q