Reputation: 145
I have a first view which is the log in screen. After the user logs in there's a button to push the view to my second view. My problem is that when I press the button which pushes back to the first view, it calls the viewDidLoad method and runs the view as though I just built the app. I have other labels that are set according to the logged in user, so I want to have my state of the first view before it was pushed to be retained.
This is the method I use to push to second view:
-(IBAction) secondView
{
_secondView = [[SecondViewController alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:_secondView animated:YES];
[_secondView release];
}
and the method to push back to first view:
-(IBAction) back {
_firstView = [[FirstViewController alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:_firstView animated:YES];
[_firstView release];
}
I'm not using navigation controller, so is there a way to push back to my first view to the way it is when I pushed to my second view? Thanks in advance.
Upvotes: 2
Views: 278
Reputation: 900
If you what to push back to the FirstViewController, use this code in the back method
[self dismissModalViewControllerAnimated:YES];
Upvotes: 3
Reputation: 4164
When you go back dismiss your SecondViewController instead of creating an instance of FirstViewController and presenting it.
Upvotes: 0
Reputation: 1266
You need to dismiss the second UIVIewController
instead of presenting the first as another modal view.
You should replace the back
method with something like this:
-(IBAction)back:{
[[self parentViewController] dismissModalViewControllerAnimated:YES];
}
That will return you to the first view as it was and (likely) deallocate the second UIViewController
.
Hope that's helpful.
Upvotes: 3
Reputation: 48398
To go back to the firstViewController, instead of pushing it onto the stack (which would then be _firstView _secondView _firstView
), pop the secondViewController with
-(IBAction) back {
[self dismissModalViewControllerAnimated:YES];
}
The problem with what you're doing is that you are creating a new firstViewController when you "go back" using your current method (alloc
and init
are the clues to this). Really, you just want to pop the secondViewController off of the top of the firstViewController, which you can do with the method above.
Upvotes: 3