UI Wiring Code

Hi


When wiring things in a Storboard and control+draging to let a button show a window in a macOS application where this code

will be added ? in which file ? can it be editied by code ?

--

Kindest Regards

Answered by Claude31 in 394690022

where does the code for first option goes ? is it in a file that we can see its source code ?


First option, you mean control-drag from the button ?


No, it is not in a separate file, it is directly in the button attributes, done automatically by compiler.


You can do the test:

- define a segue from the button

- define an IBOutlet for the button : goToSecondViewButton


In viewDidload, add:

print(goToSecondViewButton.action)


You will get:

Optional(perform:)


The action associated with the segue is perform() built in function.


If you want now to override this segue:

add in viewDidLoad:


        goToSecondViewButton.action = #selector(newAction(sender:))
        goToSecondViewButton.target = self  // Needed, otherwise, crash


and define a func for this selector in the ViewController class:

    @objc func newAction(sender: NSButton) {
        print("Overriden")
    }


Now, when you click the button, you do not segue anymore but get on console:

Overriden

If you control-drag from a button to another window, you create a segue. You can see in the connections inspector for the button that there is an action in the triggered segue.

In that case you don't have (and must not have) an IBAction for the button.


But you can as well:


- In IB: create a segue between the first and second window (control-drag from view controll blue icon in the vontroller bar, not from button)

- give an identifier such as FirstToSecondSegue

- Create an IBAction for the button

- add performSegue wiin the IBAction:

performSegue(withIdentifier : "FirstToSecondSegue", sender: nil)

Note: could set sender to firstVC

Thanks allot, but my question is where does the code for first option goes ? is it in a file that we can see its source code ?

Accepted Answer

where does the code for first option goes ? is it in a file that we can see its source code ?


First option, you mean control-drag from the button ?


No, it is not in a separate file, it is directly in the button attributes, done automatically by compiler.


You can do the test:

- define a segue from the button

- define an IBOutlet for the button : goToSecondViewButton


In viewDidload, add:

print(goToSecondViewButton.action)


You will get:

Optional(perform:)


The action associated with the segue is perform() built in function.


If you want now to override this segue:

add in viewDidLoad:


        goToSecondViewButton.action = #selector(newAction(sender:))
        goToSecondViewButton.target = self  // Needed, otherwise, crash


and define a func for this selector in the ViewController class:

    @objc func newAction(sender: NSButton) {
        print("Overriden")
    }


Now, when you click the button, you do not segue anymore but get on console:

Overriden

UI Wiring Code
 
 
Q