Content in UITextView is lost when using the back

2019-08-20 07:26发布

问题:

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?

回答1:

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.