How to segue back to a UIViewController that's already loaded?
I have a view on a storyboard the has a button to perform a certain action. To perform this action though, the user must be logged in. The button handler tests if the user is logged in and performs one segue if YES and another if NO. The NO segues pushes to a login view controller. There is another segue connecting back to the first view controller so if login is successfull the user can pick up where they left off. The view controllers are embedded in a navigation controller.
Problem is, the 'return' segue is loading a whole new instance of the view controller and not referencing the origional instance so I end up with empty interface elements and 2 copies of that view controller in memory.
How can I get back to the original instance of the view controller?
Solution 1:
Assuming by your use of "push
" you mean that you are using a UINavigationController
, then you can use the following to return to the top of the stack.
[self.navigationController popToRootViewControllerAnimated:YES];
or
UIViewController *prevVC = [self.navigationController.viewControllers objectAtIndex:<n>];
[self.navigationController popToViewController:prevVC animated:YES];
to pop to a specific level where <n>
is the level.
or
[self.navigationController popViewControllerAnimated:YES];
If you just want to pop one level back up the navigation stack.
Solution 2:
Instead of using a segue to go back to the previous controller you should use the event that was associated with the segue going back to instead dismiss the current view controller (if segue was modal) or pop this from the navigation controller (if the segue was push) and the previous controller will show automatically.
For example, if the modal segue that goes back was being performed when you pressed a button on your login screen, you delete the segue from the storyboard and on the touch up inside event of the button you link with your action that, upon successful login will call:
[self dismissViewControllerAnimated:YES completion:nil];
On the storyboard, you will right click on the button and drag the touch up inside sent event to the first responder of the view controller scene. This will execute the reverse transition that was executed when you performed the segue transition. You can use a modal segue for that, no need to use a navigation controller.
If you pushed the login view onto the navigation controller, then you have to pop it from the stack (if going back to the previous view). Use one of these:
[[self navigationController] popViewControllerAnimated:YES]; // goes back to previous view
[[self navigationController] popToViewController: myViewControllerAfterLogin animated:YES]; // goes back to specific view on stack.
[[self navigationController] popToRootViewControllerAnimated:YES]; // goes back to first view on the stack
The animated transition should reverse the type of transition used on the segue, so curl up will be curled down when you dismiss the view.
Solution 3:
If you are using a segue to go forward then you should use an unwind segue to go back. Detailed info on how to use an unwind segue is in this Technical Note TN2298 Using Unwind Segues.