How to create a button in Preferences

macOS, Swift, User Interface: Storyboard

I have two pages: ViewController and Preferences. I have a button on the Preferences window and I want to trigger an action on the ViewController window. How can I do that?

The button is in Preferences window and the action should be in the ViewController window.

(In storyboard, If I open the Assistant Editor I get Preferences.swift. I cannot make the regular connection with the control drag)

Replies

What do you mean by pages (I have two pages) ?
Preferences is a ViewController also ?
When you trigger the action in the button, do you first display preferences or want to do it while staying in Preferences ?
Please show code that will be easier to explain.

You have several ways to do it:
  • delegation: define a protocol with a func (myFunc) that you implement in ViewController ; make Preferences conform to this protocol and have a delegate (optional to ViewController class) that is set to the ViewController instance. Then call delegate?.myFunc from the IBAction.

  • notification, if both views are already loaded

  • a global reference to the ViewController that you use from Preferences

  • What do you mean by pages (I have two pages) ?

I have two View Controllers in storyboard and two files for the code: ViewController.swift and Preferences.swift. They are connected in the Identity inspector.
  • Please show code that will be easier to explain.

In a simplified version, there is no code yet. I just put a button on the View Controller - Preferences of the Storyboard