Swift 3 - Push segue to navigation controller

15,127

Solution 1

You need to embed your source view controller in a Navigation Controller or change the segue kind from push to something else. Also, try and cast your destination controller to UINavigationController to prevent a different error after the initial one if fixed:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let navVC = segue.destinationViewController as? UINavigationController{
            if let historyVC = navVC.viewControllers[0] as? HistoryController{
                historyVC.detailItem = barcodeInt as AnyObject
            }
        }
    }

See if that works for you.

EDIT: Updated the code above.

Solution 2

You should use show segue instead of push segue.

The show segue will push the next viewController into the first viewcontroller's navigationController stack whenever possible, otherwise it will use modalPresent to show the next viewController.

If you embed your 1st viewController into a UINavigationController, and still keep your Navigation Controller,then you use push segue, you will get a exception:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'

Simple change your segue from push to show should fit in your case.

Solution 3

Your problem is not in your code, that is fine, it is in your storyboard.

If you would like to push to HistoryVC from ViewController, ViewController should be embedded in a UINavigationController, not HistoryVC (it would be embedded implicitly by it's relationship with ViewController).

The reason for this is that the VC that is pushing must be embedded in a UINavigationController, the vc that is being pushed is inherently embedded because it is added to the UINavigationController's stack.

enter image description here

If the initial ViewController is not within a UINavigationController, there is no stack for the pushed ViewController (HistoryVC) to be pushed onto. Basically, you embed your starting point for your stack (Your first ViewController) in the UINavigationController.

If you want to start a new stack a few VCs down the line you can embed the next view controller in a new UINavigationController, that however you would need to segue to modally.

Solution 4

Try this one:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let navVC = segue.destinationViewController as? UINavigationController{
        if let historyVC = navVC.childViewControllers[0] as? HistoryController{
            historyVC.detailItem = barcodeInt as AnyObject
        }
    }
}
Share:
15,127

Related videos on Youtube

user979331
Author by

user979331

Updated on June 04, 2022

Comments

  • user979331
    user979331 almost 2 years

    In my storyboard I have a View Controller, I also have a Navigation Controller and another View Controller called HistoryController. The Navigation Controller and the HistroyController have a relationship "root view controller"

    I have a button on my 1st View Controller and that button has a push segue to the Navigation Controller.

    I have this code in the 1st View Controller to to prepare the segue:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
            if let viewController = segue.destination as? HistoryController {
                viewController.detailItem = barcodeInt as AnyObject
            }
    
        }
    

    my problem is when I run my code and push the button in my first controller, I get this error:

    Could not find a navigation controller for segue 'HistorySegue'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
    

    My question is, why am I getting this error and how can I fix it?

    I have tried the following

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let navVC = segue.destinationViewController as? UINavigationController{
                if let historyVC = navVC.viewControllers[0] as? HistoryController{
                    historyVC.detailItem = barcodeInt as AnyObject
                }
            }
        }
    

    I have also tried

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let nav = segue.destination as? UINavigationController {
            if let vc = nav.visibleViewController as? HistoryController {
                vc.detailItem = barcodeInt as AnyObject
            }
        }
    }
    

    and I still get the same error:

    Could not find a navigation controller for segue 'HistorySegue'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
    
  • user979331
    user979331 over 7 years
    I changed my code to yours and I get this error Value of type 'UINavigationController' has no member 'detailItem'
  • Sahil
    Sahil over 7 years
    use let controller = viewController.topViewController controller.detailItem = barcodeInt as AnyObject
  • tomalbrc
    tomalbrc over 6 years
    how did you generate this image?