Search code examples
swiftmacosappdelegatensviewcontroller

How to access NSViewController from app delegate?


In my macOS application I have menu items, which are replicated also in main UI. Application is consisted of main window with its delegate and single view along with its view controller. In app delegate I capture menu item click action, then I need to send this event to my view controller in order to take appropriate actions and also update main UI.

Question is how to access my view controller (NSViewController) from app delegate?


Solution

  • Actually you don't need a reference to the controller. There is First Responder

    • Declare the IBActions
    • In Interface Builder connect the menu items to the First Responder (red cube) of the target object and choose the appropriate action.

    The action will be executed in the first object of the responder chain which implements the action.