Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

shahab47

macrumors newbie
Original poster
Apr 8, 2012
13
0
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.
Thanks
Shahab
 
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 :)
 
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.

Code:
[segue.destinationViewController setGameSettings:self.gameSettings];

Let me know if this works for you!
 
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 are you presenting view controller 2 and how do you "come back" to view controller 1?

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.

Depends how you are using the segue. It is possible to go back using them.
 
Last edited:
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.
 
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...
Code:
[self dismissViewControllerAnimated:YES completion:nil];
 
Dismiss current vc worked.

Thanks Rob.
the dismiss current vc worked.

I used the following code :

Code:
- (IBAction)B_login:(id)sender {
    [self.presentingViewController dismissModalViewControllerAnimated:YES];
}
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.