Reputation: 1188
I'm new to swift and working on a project in swift 3.0 and I have a particular UIViewController which has a UITextField and a UIButton. Tapping on the button I'll be navigating to another UIViewController and display the text which I have entered in the previous UIViewController in the current UIViewController. Thus, in the current UIViewController I have a button that will allows me to navigate back to the previous UIViewController. As of now when I click the button to navigate back to the previous UIViewController, the text I have entered has been deleted. My requirement is to keep the text I have typed as it is even when I click to navigate back from the button. The code I'm asking to navigate back as bellow. What am I missing here ??
func segueback(action: UIAlertAction) {
let AddToPlaylistViewController: AddToPlaylistViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ShowAddAudiosToPlaylistVC") as UIViewController as! AddToPlaylistViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = AddToPlaylistViewController
}
Upvotes: 1
Views: 143
Reputation: 19750
You should use a navigation controller and when you segue, this will push
a the new controller on top of the stack, then when you wish to return you pop
the top most controller to return back to the original. keeping its state. Your application shouldn't be setting rootViewController to navigate around the app
See the Documentation here for UINavigationController usage. It maybe too much to explain here.
EDIT:
If you are already using a UINavigationController and are using a segue properly, then in your second view controller you just dismiss the view controller and it will return to the previous one.
navigationController?.popViewController(animated: true)
(Thanks to Duncan) If you are presenting your view controller modally, you should instead use
self.dismiss(animated: true)
Upvotes: 1
Reputation: 27438
You are instantiate AddToPlaylistViewController
and setting it as root view controller
that means it's creates new AddToPlaylistViewController
's instance!! so, it have all the field should have default value.
If you are using NavigationController
then you just need to go back like,
self.navigationController?.popViewController(animated: true)
If you are not using navigation controller then start to use it!!
Upvotes: 0
Reputation: 131461
Scriptable told you what to do:
func segueback(action: UIAlertAction) {
self.navigationController?.popViewController(animated: true)
}
You should accept their answer. That's just code showing what they were saying.
Note that the "self" isn't necessary above. I added it to show what's going on.
Upvotes: 1
Reputation: 835
Here you are replacing rootViewController with the new instance of AddToPlaylistViewController,so what ever data you stored previously will be lost.
Instead you can use navigation controller as rootViewController and push second viewcontroller from firstviewcontroller.To go back to first view controller you just need to pop view controller from second view controller.
In segueback method you just need to call
self.navigationController?.popViewController(animated:true)
Upvotes: 1
Reputation: 1292
Why The Text Is Cleared?
Simply, You are instantiating a new AddToPlaylistViewController to show, and as you would expect with a new view controller all values will be defaults hence the blank textField.
Solution - Using UINavigationViewController
Use a navigationViewController and pop back to the existing AddToPlaylistViewController which is still in memory.
Solution - Without UINavigationViewController
When leaving AddToPlaylistViewController and presenting a new ViewController pass self (AddToPlaylistViewController) to the newViewController. Then when you wish to go back to AddToPlaylistViewController, present the existing one that was passed in.
Upvotes: 2