I have a viewController that displays the result of a query using a tableview).
By taping on a row a I push a the childView and I set a navigationBar that contains 2 buttons on the right (Previous/Next).
My question is :
How can I switch to the previous or next “childView” when I tap on previous or next button?
I would like to have also a transition effect while the view is switching?
Any help?
I have a viewController that displays the result of a query using a tableview).
Share
I had a view that contained a list of camps, and touching one takes users to the camp details. I wanted to allow the user to swipe left and right to move through the list of camps displaying the details of each. I wanted a visual animation that showed the swipe, and that the ‘Back’ button always referred to moving back up the navigation stack, ie, returning to the list.
The first view that has the table with results is the only object that knows what “previous” and “next” mean, so they will be responsible for implementing the swipe action. Because we will be making changes to the “currently displayed” row/section in that view, while the view is not the current view, you will need to add vars/properties to track that.
Because the navigation controller can already animate view changes, I let it do the majority of work. When moving “previous”, I create a new view with the previous entry details, insert it into the navigation stack (between the LIST view and the current DETAIL view), and do a popViewControllerAnimated which provides a visual effect and also unloads the details view just animated off.
To move to the “next” camp details, I create a new view with the next entry details, add it onto the end of the navigation stack, it animates in, then I clean up the navigation stack by removing the details view that just animated off.
So, in a nutshell:
The details view will detect the swipe gestures and inform the parent.
The parent determines the next/previous row that should be displayed.
The parent replaces the current view with a new one, animating the replacement left/right to visually indicate the effect. The parent also updates the navigation stack to ensure that the “Back” button always acts as a pop to the LIST view, not to a previously shown DETAIL view.
I can’t post all my code here, but below is the majority. Some specific GOTCHAS to watch out for:
Manipulating the navigation stack can cause warning errors at runtime if you try to delete a VC while it is animating out. Therefore we wait until it is completely replaced, then remove it, by registering as the delegate for the navigation controller, and using the “didShowViewController” method to detect when it is safe to make changes. This complication is only needed when moving forward in the list, since in the “back” logic, the navigation controller cleans up itself after the popViewController.
In order to use didShowViewController you must set a delegate. The delegate must not be a VC that may go away, otherwise you will get a crash. I only have the controlling LIST view set itself as a delegate.
As you manipulate which row/section the user is viewing the details of, I also move the highlight (and scroll the table) on the hidden LIST view, so that when they eventually go “BACK” to it, the last viewed item is shown.
When creating a DETAIL view, pass in the parent, define methods to let the parent know a swipe occurred, and register swipe recognizers on the DETAIL view, in the viewDidLoad method,
Code in the LIST view (parent)
Code in the DETAILS view (child):