Search code examples
iosswiftsegue

Push Action Segue from button to UIViewController


I am new to iOS app development and currently facing some issues segueing from a UIButton to a UIViewController. I did above in Main.storyboard by:

  • selecting UIButton
  • holding "control" and dragging to UIViewController
  • Selecting "push" under Action Segue

After running this, i get below error

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Push segues can only be used when the source controller is managed by an instance of UINavigationController.'

Need help on resolving above error. Would appreciate it if solution comes with screenshots since i'm a newbie. Thanks!

After implementing @technerd solution, it still does not work. Below are images of the Navigation Controller menu and Action Segue Menu

enter image description here

enter image description here


Solution

  • From error logs it looks like you have not embedded UINavigationController as initial controller in you storyboard.

    So first set UINavigationController as initial controller. Because push and pop operation only performed when there is Navigation stack available.

    1. Select first ViewController in Storyboard as per screen shot.
    2. Then Editor -> EmbedIn -> NavigationController

    To know how Navigation Stack works, go through this link.

    enter image description here

    Once you set Navigation as InitialViewController, then set your first viewcontroller you want to show first as rootViewController of that navigation controller.

    enter image description here

    Then you can drag push from button to secondViewController to push from firstViewController to secondViewController.

    enter image description here

    As you can read in screenshot that Push(Deprecated), so you have to use Show relationship. Show also perform same operation as Push.

    Update :

    As per your question update, you are facing that you are not able to show other Action Segue. Follow below steps to make it available.

    1. Select your Navigation Controller in storyboard.
    2. Enable Use Trait Variations option under Interface Builder Document on right side panel.

    enter image description here

    For more details check this thread.