-1

I Have UINavigationController and it's root view is UICollectionViewController, Each time i tap on cell in this UICollectionViewController pushes another UICollectionViewController which have a UITextFiled The Problem is when i go back to the root and tap the same cell it's will push WHOLE NEW view, and UITextField is empty.

the thing i want is similar to whatsapp or other chat apps, when open Chat conversation and type something and go back to the same chat conversation the UITextField will have the text i've wrote.

EDIT: this is the code i use to push the controller:

    let nextView = self.storyboard!.instantiateViewControllerWithIdentifier("nextView") as! nextViewController
    self.navigationController!.pushViewController(nextView, animated: true)

Can i prevent the UINavigationController From pop up the view, and keep it loaded?

  • You are basically describing normal nav controller behavior. Once a view is popped off the stack, the memory is re-allocated and the view is completely gone. If you want to restore data when tapping again, you'll need to devise a way to pass this data in to your new view controller. To get a better answer, try adding some code showing how you initialize your view and what you need accomplished. Otherwise your question may be too ambiguous to get any good answers. – Bill Burgess Sep 05 '16 at 11:44
  • you need to implement protocol or notification in pushed view controller and before you pop call delegate – Prashant Tukadiya Sep 05 '16 at 11:55
  • I've updated my question, Can i prevent the UINavigationController From pop up the view, and keep it loaded? – Haidar Mahmoud Sep 05 '16 at 11:56

3 Answers3

0

Use local db to save unsaved data. On view appear for chat view check whether any unsaved data present for that particular user, if that the case then add it in the text-box.

0

This is the default or normal behavior of UINavigationController. When you pop any ViewController then it's removed from the navigation stack and your ViewController deallocate from the memory.

So, if you want to achieve to keep textfield's text as you mentioned then you should have store it somewhere. You can store in NSUserDefaults and for same key. Then check in viewDidload every time if there is text then show to textfield and every viewDidDisappear set userdefault again with new edit and if textfield is empty then set empty string.

Second thing you can use delegate and protocols to send data back to previous class and again send it to that class or VC and show it!!

You can manage this stuff something like with this kind of solution!

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
-1

User Protocols to pass data from second view to first view. Following link will help you, and using protocol, you can pass value and show as your defined.

Do as

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    if self.isBeingDismissed() {
        self.delegate?.acceptData(textFieldOutlet.text)
    }
}

https://www.codebeaulieu.com/36/Passing-data-with-the-protocol-delegate-pattern

This delegation is proper way to pass value from second controller to first controller.

One more thing, you must re-consider your view controllers architecture. How best should it be for a kind of chat, you are making.

Thanks.

Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154