Adding further control elements in code
Let's now add a button, programmatically of course, with which the user can alphabetically sort the table view's rows and the underlying data.
Adding the sort button
Firstly, we need to build the button itself.
Add the following method inside the buildUI
method of the ViewController
class:
func addSortButton() { let frame = CGRect(x: 268, y: 178, width: 192, height: 32) let sortButton = NSButton(frame: frame) sortButton.bezelStyle = .rounded sortButton.title = "Sort" view.addSubview(sortButton) }
And add a call to that method within the buildUI
method:
addTable() configureTable() addInfoLabel() addSortButton()
The interface should now look as follows:
It's a fine looking button, but it doesn't do anything yet. For this, we need to add a target
and an action
to the button.
Add the following lines of code to the...