0

I have an iOS app using a tabBarController. Each tabs root view is a navigation controller. In one of the tabs I use a button on the navigation bar to push me to UIViewController that contains a UITextView.

The problem I am struggling with is that any content added to the UITextView is lost when back button on the navigation bar is pushed. I have found a few similar questions all with drastically different solutions. I am not sure how to proceed from here.

To be clear on what I would like to do. The user can input some text into the UITextView and navigate back to the first view without losing it. When they choose to go back into the 2nd view the text should be in the UITextView.

content handling of UITextView between two navigation view suggested using the delegate class's string to retain the text. I am concerned here because I'm not sure if it will maintain the possible paragraph formatting from the user.

I am already using core data throughout the app so I could easily create an attribute to store this in. I'm just not sure what the appropriate/best/easiest way is.

What is the best way to accomplish what I am trying to do? I am kind of new to this so Snippets really help!

UPDATE More pertinent information

The text in the UITextView is meant to ultimately be posted to a server. I just need to hold onto that text until they post it. The app has a login so there is always the potential that they could log off and on again, or even someone else. If I understand things correctly this will cause a new managedObjectContext to be created therefore wiping out all core data I had stored for the individual as well as NSUserDefaults. Is there a way to store the text through the process I described above?

Community
  • 1
  • 1
Ben
  • 967
  • 3
  • 9
  • 23
  • Once you hit back, your view controller containing the UITextView has been popped off the stack and effectively deallocated. So what you are experiencing is the expected behavior. Now if you want to retain that NSString, you will have to save it somehow. I'm not sure how that NSString is being used so you could either save it in core data or simply just set it in NSUserDefaults. Again, it really depends on how you use the data. You could also save it in some resource singleton class. – rocky Jul 19 '13 at 22:49
  • @rocky thank you for pointing out the expected behavior of the view controller. Please see the update I added to the question and give me your thoughts. – Ben Jul 20 '13 at 02:05

1 Answers1

0

You describe the text only being used in one view controller with no need to pass it back to the other view controller.

So, you can get the attributed string from the text view (either when the editing session is completed or when the view is removed from the screen). It conforms to NSCoding so you can convert it into data and save that data to disk / database.

Later, when the view is re shown you can load the data from disk and recreate the text for display.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Wain, you are correct the text will only be used in the one view controller. Just glancing over the NSCoding material it seems very simple to implement. I do have one question regarding it, Do I need to create a custom NSObject class to implement the `encodeWithEncoder:` and `initWithEncoder:` methods or can I just add them into my `UIViewController` class? – Ben Jul 20 '13 at 01:41
  • 2
    I believe Wain is suggesting to just save the attributed string (NSAttributedString), which implements coding already. – Steven McGrath Jul 20 '13 at 05:33
  • Thanks for pointing that out @StevenMcGrath! After reading your statement It dawned on me "conforms to NSCoding" means exactly what it says, not to use the NSCoding methods. I ended up just saving the string to NSUserDefaults when the view disappears and reloading in in viewDidLoad. Pretty simple fix, not bullet proof but will serve the purpose for now. – Ben Jul 26 '13 at 03:33