Balázs Vincze
Balázs Vincze

Reputation: 3840

Preserve state of View Controller when using segues

I have a View Controller (A) with a text field and some other things in it.

When the user presses a button on View Controller A it segues to View Controller B using "Present Modally".

How could I preserve the state of View Controller A (e.g. text in the textfield) when returning to it from View Controller B. I would rather avoid using NSUserDefaults if possible.

Thanks!

Upvotes: 0

Views: 149

Answers (1)

Duncan C
Duncan C

Reputation: 131491

When you present view controller B modally on top of view view controller A, view controller A is not closed - it's just covered by view controller B. The close action on view controller B should invoke dismiss(animated:completion:) to dismiss the modal. When you do that you can be certain that view controller A will be revealed with its state intact.

You should NOT use a segue to go back to view controller A. That would create a new copy of view controller A that would wind up being displayed on top of the original view controller A and the new view controller B. That is a bad idea.

Upvotes: 3

Related Questions