view controller doesn't retain its fields status

Discussion in 'iOS Programming' started by shahab47, Apr 11, 2012.

  1. shahab47 macrumors newbie

    Apr 8, 2012
    Hello there.

    I am beginner.
    I just have a switch and a text field and a button on one 1st view controller.
    If I click on button, 2nd view controller pops up.

    Now if I write something in text field of 1st controller and changed the switch position, and then go to view controller 2 and come back to view controller1, the text field content is gone and also switch is back to its default state,

    How do I make the view controller remember its last state?

    Any help would be appreciated.
  2. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    errr, it shouldn't be "gone", unless you explicitely set it in the viewWillAppear/viewWillDisappear.
    Thing is, your controller won't remove it's texts unless your app doesn't have memory anymore, and will release some front end of the controllers (easy explained). This could cause of the loss of data entered.
    You need to save your things to a database/userdefaults/Keychain, whatever. So you can re enter it if you want to remember it :)
  3. RobMSantos macrumors newbie


    Oct 4, 2009
    Greater St. Louis, USA
    If you are using segues, you aren't actually going "back" to view controller one. You are creating a new instance of view controller one and then putting it on the screen.

    The way I've been handling your situation is I have each viewController pass a settings object to each other on segue so that viewController 2 knows what settings it got from #1 and #1 can receive what it sent to #2.

    [segue.destinationViewController setGameSettings:self.gameSettings];
    Let me know if this works for you!
  4. dejo, Apr 12, 2012
    Last edited: Apr 12, 2012

    dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    How are you presenting view controller 2 and how do you "come back" to view controller 1?

    Depends how you are using the segue. It is possible to go back using them.
  5. shahab47 thread starter macrumors newbie

    Apr 8, 2012
    Hello Guys.

    I have two buttons on vc1. (viewcontroller1).
    One is used to go to vc2 via a custom segue.
    Second button is used to go to vc2 directly using "modal" segue"

    On vc2, there is just one back button, which takes you back to vc1 with "modal" segue.

    ROB: Where should I write the statement you mentioned in your reply? Should I write in the .m file for both the vc? Should it be included in any particular method?

    I tried in my custom segue. But it didn't work. The code gave error. I tried to write in vc also, but didn't work. maybe I am not writing it correctly.

    Thanks a lot everybody for your feedback.
  6. RobMSantos macrumors newbie


    Oct 4, 2009
    Greater St. Louis, USA
    I put that code within the prepareForSegue:sender: method.

    Also Dejo, is right, you could also tell the view controller to dismiss itself. His way is probably the technique you want to use...
    [self dismissViewControllerAnimated:YES completion:nil];
  7. shahab47, Apr 13, 2012
    Last edited by a moderator: Apr 13, 2012

    shahab47 thread starter macrumors newbie

    Apr 8, 2012
    Dismiss current vc worked.

    Thanks Rob.
    the dismiss current vc worked.

    I used the following code :

    - (IBAction)B_login:(id)sender {
        [self.presentingViewController dismissModalViewControllerAnimated:YES];

Share This Page