➤ Control-drag from the “Icon” table view cell to the Icon Picker View Controller and add a segue of type Selection Segue – Show. (Make sure you’re dragging from the Table View Cell, not its Content View or any of the other subviews. If you are unable to do this accurately from the scene, remember that you can also Control-drag from the Document Outline.) raywenderlich.com 424
➤ Give the segue the identifier PickIcon.
➤ Thanks to the segue, the new view controller has been given a navigation bar. (However, it might not have a Navigation Item - if it doesn’t, drag one from the Object Library on to the Icon Picker scene.) Double-click the navigation bar and change its title to Choose Icon.
When you push a ViewController onto the NavigationController’s “stack” (an array of ViewControllers), by default you get a <Back button. The NavigationController handles those details for you.
The back button gets the title of the parent view controller if the parent view controller has a title set. If not, the button shows “< Back”. So, if you want the button to show “< Back” explicitly, then remove the title from the view controller.
I was able to set the back button’s text dynamically by performing the segue with code. In the storyboard, delete the current segue, then look at the drawing for TableViewController1 and control drag from the little yellow icon at the top to the middle of the of TableViewController2, and from the popup menu select Manual Segue > Show. Click on the segue and give it an identifier name. Now, you can implement the segue in code:
Interestingly, when I tried setting the backBarButtonItem in TableViewController1’s viewDidLoad() method, the custom text would not get displayed for the back button. For some reason, viewDidLoad() is too early to set the backBarButtonItem.
If you don’t actually need to set the back button dynamically, then you can set the custom text for the back button in the storyboard. Select the NavigationItem for TableViewController1 (it has a left facing arrow for an icon in the storyboard’s table of contents), then open the Attributes Inspector and change the “Back Button” field (be sure to tab out of that field for the change to take effect).
Here’s how to accomplish the same thing inside TableViewController2:
class MySecondTableViewController: UITableViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let myBackButton = UIBarButtonItem(
title: "<Custom text",
style: .plain,
target: self,
action: #selector(goBack) //selector() needs to be paired with an @objc label on the method
)
navigationItem.leftBarButtonItem = myBackButton
}
@objc func goBack() {
navigationController?.popViewController(animated: true)
}
In this case, to connect the two TableViewControllers, in the storyboard control drag from the TableViewCell in TableViewController1 to the middle of TableViewController2 and in the popup menu select Selection Segue > Show.