Search code examples
iphoneswiftanimationios9uistoryboardsegue

Segue using wrong animation and back button not showing up


I am using a UINavigationController inside a UITabBarController in my project. Inside this UINavigationController there is an UICollectionView. In the UICollectionView, after tapping on an item in the collection a show segue should be performed to see some kind of detail page from where you can go either back or to another collection with a different data-filter applied.

Being new to iOS development, I struggle with getting the things to behave as I want. Even though all segues are set to show in the storyboard they animate as modal animation (coming in from the bottom instead of the right side).

Also the back button does not appear. I set text for it in the attribute inspector (I already know that these have to be in the view that 'goes away') and also added titles to all of the navigation items… still no sign of them showing up.

Here is my storyboard:storyboard

This is the prepareForSegue I use:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showService" {
        if let indexPath = self.collectionView?.indexPathsForSelectedItems() {
            let object = serviceData[indexPath.last!.row]
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! ServiceDetailViewController
            controller.serviceItem = object
            controller.callerItem = self.name
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}

I fire the segue in collectionView:didSelectItemAtIndexPath: of my collection view:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let cell = self.collectionView?.cellForItemAtIndexPath(indexPath)
    performSegueWithIdentifier("showService", sender: cell)
}

Solution

  • The problem is that your segue runs from a navigation controller's child ("Another Title") to a navigation controller. That is not how you construct a push/show segue within a navigation interface. You need to run from the navigation controller's child to another child, that is, e.g. to your "Title" view controller. Thus, if (let's say) your push/show segue runs from "Another Title" to "Title", then when that segue is triggered, the "Title" view controller will be pushed onto the same navigation controller's stack as the "Another Title" view controller, and so you'll get the title change in the nav bar and the back button.