Reputation: 363
I hope an easy question (but I can't solve it :)) I have 2 views, On view 1 I have UILabels that I change the text on with a button click, if I navigate to view 2 and then go back to view 1 the text is reset.
Is there any way to retain the text on the label when the view is changed?
Originally I used:
-(IBAction) napButton:
(id) sender{ nap *nap1 = [[nap alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:nap1 animated:YES];
this performed a simple view switch. With the suggestion below I changed this to:
-(IBAction) napButton:
(id) sender{ nap *nap1 = [[nap alloc] initWithNibName:@"nap" bundle:nil];
[self.navigationController pushViewController:nap1];
[nap1 release]; } }
The button performs no action and I get warning UINavigationController may not respond to -pushViewController
Upvotes: 0
Views: 460
Reputation: 73588
use UINavigationController
. In a memory constrained device this is the best way to push or pop new views while preserving data or state in previous view.
An instance of UINavigationController
can be created either in code or in an XIB file with relative ease. It’s thought of as a stack: it has a root view controller, and then new view controllers can be pushed onto the stack (often when the user taps on a row in a table) or popped off the stack (often by pressing the back button).
There are 4our methods are used to navigate user through the stack:
– pushViewController:animated:
– popViewControllerAnimated:
– popToRootViewControllerAnimated:
– popToViewController:animated:
For example if you want to move from view1 to view2, you need to do this from view1 -
SecondViewController *secondView = [[SecondViewController alloc]
initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondView];
[secondView release];
to move back to the previous state, i.e. from view2 to view1, you need to do this from view2 -
[self.navigationController popViewControllerAnimated:YES];
This should solve your problem in the most efficient way.
Upvotes: 1