Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 887
TijuanaKez Avatar asked Apr 04 '12 00:04

TijuanaKez


People also ask

How do you unwind segues in Swift?

Connect a triggering object to the exit control In your storyboard, create an unwind segue by right-clicking a triggering object and dragging to the Exit control at the top of your view controller's scene.

What is the difference between viewController and UIViewController?

An UIViewController is just the base class of this already defined "View Controller", you add a viewController as you said, but a viewController has a class associated to it, that can be a UIViewController, UITableViewController, or variations/subclasses.

How do I add a viewController to the tab bar controller in storyboard?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

What is a UIViewController?

A UIViewController is an object which manages the view hierarchy of the UIKit application. The UIViewController defines the shared behavior and properties for all types of ViewController that are used in the iOS application. The UIViewController class inherits the UIResponder class.


2 Answers

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.

like image 111
Mick MacCallum Avatar answered Sep 22 '22 04:09

Mick MacCallum


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.

like image 20
TRVD1707 Avatar answered Sep 26 '22 04:09

TRVD1707