IconPicker storyboard back button

hi again

sorry for all the (probably dumb) questions…

the IconPicker storyboard has a “< Back” button in the navbar but i cannot see how to get that

i can get “cancel done”, i can get “< add checklist” but the “< Back” as it is in the book is completely stumping me

any help appreciated
thanks

Did you do this (p. 424-425):

➤ 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.

hi

yes i did and i get a “< Add Checklist” item in the navbar but i cannot find how to change it to “< Back” like in the book

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.

ahhhhhhh so stupid - sorry for wasting your time
:frowning:

Nice. Works for me. (And so does changing the title of the ViewController to “Back”. :slight_smile: )

ahhhhhhhh nice - then i can set it dynamically if i want without the “< Back” being affected
:slight_smile:

ummmmm no i can’t sigh oh well live and learn :slight_smile:

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:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let myBackButton = UIBarButtonItem()
    myBackButton.title = "Custom text"
    navigationItem.backBarButtonItem = myBackButton

    performSegue(withIdentifier: "ShowMyCustomBackButton", sender: nil)
}

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.

This topic was automatically closed after 166 days. New replies are no longer allowed.