Search code examples
iosxcodeuinavigationcontrolleruitextfieldpopviewcontrolleranimated

UITextField contents are empty after popViewControllerAnimated


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];
}

Solution

  • 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.