使用导航控制器中的后退按钮时,UITextView 中的内容丢失 [英] Content in UITextView is lost when using the back button in my navigation controller

查看:30
本文介绍了使用导航控制器中的后退按钮时,UITextView 中的内容丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 tabBarController 的 iOS 应用程序.每个选项卡根视图都是一个导航控制器.在其中一个选项卡中,我使用导航栏上的按钮将我推送到包含 UITextView 的 UIViewController.

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.

我遇到的问题是,当导航栏上的后退按钮被按下时,添加到 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.

明确我想做什么.用户可以在 UITextView 中输入一些文本并导航回第一个视图而不会丢失它.当他们选择返回到第二个视图时,文本应该在 UITextView 中.

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.

两个导航视图之间 UITextView 的内容处理建议使用委托类的 string 来保留文本.我在这里很担心,因为我不确定它是否会保持用户可能使用的段落格式.

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!

更新更多相关信息

UITextView 中的文本最终会发布到服务器.我只需要保留该文本,直到他们发布为止.该应用程序有一个登录名,因此他们总是有可能注销并再次登录,甚至是其他人.如果我理解正确,这将导致创建一个新的 managedObjectContext,从而清除我为个人以及 NSUserDefaults 存储的所有核心数据.有没有办法通过我上面描述的过程来存储文本?

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?

推荐答案

您描述的文本仅在一个视图控制器中使用,无需将其传回另一个视图控制器.

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

因此,您可以从文本视图中获取属性字符串(在编辑会话完成时或当视图从屏幕上移除时).它符合NSCoding,因此您可以将其转换为数据并将该数据保存到磁盘/数据库中.

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.

这篇关于使用导航控制器中的后退按钮时,UITextView 中的内容丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆