Search code examples
iosswift3uitoolbar

How can I get a UIToolBar with buttons created from a view controller to another?


I am trying to create a second view controller and I would like to have the same toolbar with buttons that I created and coded from the first view controller without creating and coding that toolbar again. How can I do that?


Solution

  • A better approach would be to create the toolbar in a parent view controller. You should use a navigation controller embedded in a container view for that.

    This is the scene structure of this approach in Interface Builder:

    scene structure

    View structure (click to enlarge):

    view structure

    Use an Embed Segue to display the navigation controller in the container view.

    create embed segue from the container view

    select Embed in the popup

    You can use delegates to pass actions (e.g. a bar button item was tapped).
    Read more about delegates in Swift here.