0

In my app I'm using a navigation controller. My problem is, when I go to previous view and then back to my current view, all TextFields in this view are empty.

I don't know how to save these values (in textfields) when I navigate through the stack from navigation controller.

This is my method where I call popViewControllerAnimated:

- (IBAction)swipeBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
Maxim Mikheev
  • 1,984
  • 3
  • 16
  • 33
gpichler
  • 1,997
  • 2
  • 23
  • 44

4 Answers4

1

When you push or pop a view controller, it is loaded or unloaded from memory, respectively. You need to save the information before popping the controller and load the information after allocating a new one and before pushing it onto the stack. You can use NSUserDefaults or basic file I/O to save and reload your information.

Saving:

  • Save data using one of the methods above.
  • Then pop the view controller.

Loading:

  • Load the data.
  • Allocate a view controller.
  • Pass the data to the view controller (using properties, singletons, etc.)
  • Push the view controller.
Evan Mulawski
  • 51,888
  • 11
  • 110
  • 142
  • thank you for your answer, is there no other way? I have a lot of textfields in every view, this is really costly. Is there a possibility that i navigate otherwise to the previous view, so i would not have to pop it from the stack. – gpichler Jun 18 '12 at 14:06
  • You need to save data in between pushing and popping (going back) view controllers. Otherwise, the data will be lost. – Evan Mulawski Jun 18 '12 at 15:48
1

In your previous view's .h file declare a property:

@property (noonatomic, retain) YOURVIEWCONTROLLER *secondVC;

then in your @implementation, synthesize the property:

@synthesize secondVC;

then replace the code, where you're presenting the view controller as modal view, with this:

if(!secondVC){
    YOURVIEWCONTROLLER *controller=[[YOURVIEWCONTROLLER alloc]init.......];
    [self setSecondVC:controller];
    [controller release];//if you ain't using arc else just set it to nil
    controller=nil;
}
[self presentModalViewController:self.secondVC animated:YES];

'YOURVIEWCONTROLLER' is the name of the second View controller's class which contains textfield. Dont't forget to release the secondVC property in -dealloc. Hope it'd help.

Ahmed
  • 772
  • 1
  • 9
  • 26
0

You have two things to tackle here: 1. Saving the content of the text fields. 2. Restoring the content of the text fields when the view controller is pushed again.

The first one you can resolve by saving the text field values to a global object, like AppDelegate or any other global singleton.

The best way to deal with 2 is to create the initializer for the view controller with text fields. For example:

- (id)initWithOptions:(NSDictionary*)options;

Then, when you need to push the controller on the stack, you get the previously saved values from the global storage from 1, and pass them in options dictionary. Then, in viewDidLoad, you assign the values to the fields.

lawicko
  • 6,966
  • 2
  • 34
  • 47
0

Why don't you set it to a property to store it? When you want to push it to show, you can alloc it only when it is a nil member.

Siam
  • 109
  • 2
  • could you give me a code example please, i don't know how to manage it with a property. thank you – gpichler Jun 18 '12 at 14:40
  • How can I give you a demo? just like this:if (nil == self.customViewController){CustomViewController *aCustom = [[CustomViewController alloc] init]; self.customViewController = aCustom; [aCustom release];} [self.navigationController pushViewController:self.customViewController animated:YES]; – Siam Jun 19 '12 at 03:23