如何解决如何在UITextView中保存输入? 1理想的方法: 2快速解决方案:
一旦我的应用程序被两次轻按/强制退出,如何将输入保存在UITextView中?我可以在textViewDidChange
调用上编写一些可以自动保存输入的代码吗?
注意:最后一个代码部分是UITextView所在的位置。
struct ContentView: View {
@EnvironmentObject var obj : observed
@Observedobject var date1 = SaveSettings()
@Observedobject var date2 = SaveSettings()
@Observedobject var date3 = SaveSettings()
@Observedobject var date4 = SaveSettings()
@Observedobject var date5 = SaveSettings()
@Observedobject var date6 = SaveSettings()
@Observedobject var date7 = SaveSettings()
@Observedobject var date8 = SaveSettings()
@Observedobject var date9 = SaveSettings()
@Observedobject var date10 = SaveSettings()
@Observedobject var reason1 = SaveSettings()
@Observedobject var reason2 = SaveSettings()
@Observedobject private var keyboard = KeyboardResponder()
var body: some View {
TabView {
ScrollView(.vertical) {
vstack {
HStack {
Text("My journey").font(.largeTitle).foregroundColor(.blue).bold() }
// Entry 1
HStack {
Text("Date: ").foregroundColor(.blue).fontWeight(.bold).multilineTextAlignment(.trailing)
TextField("Month/Day/Year",text: $date1.date)
.padding(.bottom,8.0)
.frame(width: 170.0)
.textFieldStyle(RoundedBorderTextFieldStyle())
Spacer()
}
.padding(.leading)
vstack {
HStack {
DropDown()
}
HStack {
Text("What's going on?")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(Color.purple)
.padding(.all,8.0)
Spacer()
Button("Done") {
self.hideKeyboard()
}
Spacer()
}
HStack {
MultiTextField().frame(height : self.obj.size < 150 ? self.obj.size : 150).padding(10).background(Color.gray).cornerRadius(10)
}.padding(EdgeInsets(top: 0,leading: 25,bottom: 33,trailing: 25))
// Entry 2
HStack {
Text("Date: ").foregroundColor(.blue).fontWeight(.bold).multilineTextAlignment(.trailing).padding([.top,.leading])
TextField("Month/Day/Year",text: $date2.date2)
.padding(.vertical,8.0)
.frame(width: 170.0)
.textFieldStyle(RoundedBorderTextFieldStyle())
Spacer()
}
vstack {
HStack {
DropDown2()
}
HStack {
Text("What's going on?")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(Color.purple)
.padding(.all,8.0)
Spacer()
Button("Done") {
self.hideKeyboard()
}
Spacer()
}
HStack {
MultiTextField().frame(height : self.obj.size < 150 ? self.obj.size : 150).padding(10).background(Color.gray).cornerRadius(10)
}
.padding(EdgeInsets(top: 0,trailing: 25))
// Entry 3
HStack {
Text("Date:").foregroundColor(.blue)
.bold().padding(.leading)
TextField("Month/Day/Year",text: $date3.date3)
.frame(width: 170.0)
.textFieldStyle(RoundedBorderTextFieldStyle())
Spacer()
}
HStack {
DropDown3()
}
HStack {
Text("What's going on?")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(Color.purple)
.padding(.all,trailing: 25))
Group {
// Entry 4
HStack {
Text("Date: ").foregroundColor(.blue).fontWeight(.bold).multilineTextAlignment(.trailing)
TextField("Month/Day/Year",text: $date4.date4)
.padding(.bottom,8.0)
.frame(width: 170.0)
.textFieldStyle(RoundedBorderTextFieldStyle())
Spacer()
}
.padding(.leading)
vstack {
HStack {
DropDown4()
}
}
HStack {
Text("What's going on?")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(Color.purple)
.padding(.all,8.0)
Spacer()
Button("Done") {
self.hideKeyboard()
}
Spacer()
}
HStack {
MultiTextField().frame(height : self.obj.size < 150 ? self.obj.size : 150).padding(10).background(Color.gray).cornerRadius(10)
}
.padding(EdgeInsets(top: 0,trailing: 25))
}
}
}
}
}.padding(.bottom,keyboard.currentHeight)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct DropDown: View {
@State var expand = false
@Observedobject var feeling = SaveSettings()
var body: some View {
vstack() {
vstack(spacing: 30) {
HStack() {
Text("I am feeling: ").bold().foregroundColor(.white)
Text(feeling.feeling).bold().foregroundColor(.white)
Image(systemName: expand ? "chevron.up" : "chevron.down").resizable().frame(width: 13,height:6)
}.onTapGesture {
self.expand.toggle()
}
if expand {
Button(action: {
self.feeling.feeling = "Happy"
self.expand.toggle()
}) {
Text("Happy")
}.foregroundColor(.white)
Button(action: {
self.feeling.feeling = "Frustrated"
self.expand.toggle()
}) {
Text("Frustrated")
}.foregroundColor(.white)
Button(action: {
self.feeling.feeling = "disappointed"
self.expand.toggle()
}) {
Text("disappointed")
}.foregroundColor(.white)
Button(action: {
self.feeling.feeling = "Excited"
self.expand.toggle()
}) {
Text("Excited")
}.foregroundColor(.white)
Button(action: {
self.feeling.feeling = "Amused"
self.expand.toggle()
}) {
Text("Amused")
}.foregroundColor(.white)
Button(action: {
self.feeling.feeling = "AnxIoUs"
self.expand.toggle()
}) {
Text("AnxIoUs")
}.foregroundColor(.white)
Button(action: {
self.feeling.feeling = "Confused"
self.expand.toggle()
}) {
Text("Confused")
}.foregroundColor(.white)
Button(action: {
self.feeling.feeling = "Admiration"
self.expand.toggle()
}) {
Text("Admiration")
}.foregroundColor(.white)
Button(action: {
self.feeling.feeling = "Embarassed"
self.expand.toggle()
}) {
Text("Embarrased")
}.foregroundColor(.white)
Group {
Button(action: {
self.feeling.feeling = "Nostalgic"
self.expand.toggle()
}) {
Text("Nostalgic")
}.foregroundColor(.white)
Button(action: {
self.feeling.feeling = "Surprised"
self.expand.toggle()
}) {
Text("Surprised")
}.foregroundColor(.white)
}
}
}
}.padding()
.background(LinearGradient(gradient: .init(colors: [.blue,.purple]),startPoint: .top,endPoint: .bottom)).cornerRadius(15).shadow(color: .gray,radius: 5).animation(.interactiveSpring())
}
}
struct DropDown2: View {
@State var expand = false
@Observedobject var feeling2 = SaveSettings()
var body: some View {
vstack() {
vstack(spacing: 30) {
HStack() {
Text("I am feeling: ").bold().foregroundColor(.white)
Text(feeling2.feeling2).bold().foregroundColor(.white)
Image(systemName: expand ? "chevron.up" : "chevron.down").resizable().frame(width: 13,height:6)
}.onTapGesture {
self.expand.toggle()
}
if expand {
Button(action: {
self.feeling2.feeling2 = "Happy"
self.expand.toggle()
}) {
Text("Happy")
}.foregroundColor(.white)
Button(action: {
self.feeling2.feeling2 = "Frustrated"
self.expand.toggle()
}) {
Text("Frustrated")
}.foregroundColor(.white)
Button(action: {
self.feeling2.feeling2 = "disappointed"
self.expand.toggle()
}) {
Text("disappointed")
}.foregroundColor(.white)
Button(action: {
self.feeling2.feeling2 = "Excited"
self.expand.toggle()
}) {
Text("Excited")
}.foregroundColor(.white)
Button(action: {
self.feeling2.feeling2 = "Amused"
self.expand.toggle()
}) {
Text("Amused")
}.foregroundColor(.white)
Button(action: {
self.feeling2.feeling2 = "AnxIoUs"
self.expand.toggle()
}) {
Text("AnxIoUs")
}.foregroundColor(.white)
Button(action: {
self.feeling2.feeling2 = "Confused"
self.expand.toggle()
}) {
Text("Confused")
}.foregroundColor(.white)
Button(action: {
self.feeling2.feeling2 = "Admiration"
self.expand.toggle()
}) {
Text("Admiration")
}.foregroundColor(.white)
Button(action: {
self.feeling2.feeling2 = "Embarassed"
self.expand.toggle()
}) {
Text("Embarrased")
}.foregroundColor(.white)
Group {
Button(action: {
self.feeling2.feeling2 = "Nostalgic"
self.expand.toggle()
}) {
Text("Nostalgic")
}.foregroundColor(.white)
Button(action: {
self.feeling2.feeling2 = "Surprised"
self.expand.toggle()
}) {
Text("Surprised")
}.foregroundColor(.white)
}
}
}
}.padding()
.background(LinearGradient(gradient: .init(colors: [.blue,radius: 5).animation(.interactiveSpring())
}
}
struct DropDown3: View {
@State var expand = false
@Observedobject var feeling3 = SaveSettings()
var body: some View {
vstack() {
vstack(spacing: 30) {
HStack() {
Text("I am feeling: ").bold().foregroundColor(.white)
Text(feeling3.feeling3).bold().foregroundColor(.white)
Image(systemName: expand ? "chevron.up" : "chevron.down").resizable().frame(width: 13,height:6)
}.onTapGesture {
self.expand.toggle()
}
if expand {
Button(action: {
self.feeling3.feeling3 = "Happy"
self.expand.toggle()
}) {
Text("Happy")
}.foregroundColor(.white)
Button(action: {
self.feeling3.feeling3 = "Frustrated"
self.expand.toggle()
}) {
Text("Frustrated")
}.foregroundColor(.white)
Button(action: {
self.feeling3.feeling3 = "disappointed"
self.expand.toggle()
}) {
Text("disappointed")
}.foregroundColor(.white)
Button(action: {
self.feeling3.feeling3 = "Excited"
self.expand.toggle()
}) {
Text("Excited")
}.foregroundColor(.white)
Button(action: {
self.feeling3.feeling3 = "Amused"
self.expand.toggle()
}) {
Text("Amused")
}.foregroundColor(.white)
Button(action: {
self.feeling3.feeling3 = "AnxIoUs"
self.expand.toggle()
}) {
Text("AnxIoUs")
}.foregroundColor(.white)
Button(action: {
self.feeling3.feeling3 = "Confused"
self.expand.toggle()
}) {
Text("Confused")
}.foregroundColor(.white)
Button(action: {
self.feeling3.feeling = "Admiration"
self.expand.toggle()
}) {
Text("Admiration")
}.foregroundColor(.white)
Button(action: {
self.feeling3.feeling3 = "Embarassed"
self.expand.toggle()
}) {
Text("Embarrased")
}.foregroundColor(.white)
Group {
Button(action: {
self.feeling3.feeling3 = "Nostalgic"
self.expand.toggle()
}) {
Text("Nostalgic")
}.foregroundColor(.white)
Button(action: {
self.feeling3.feeling3 = "Surprised"
self.expand.toggle()
}) {
Text("Surprised")
}.foregroundColor(.white)
}
}
}
}.padding()
.background(LinearGradient(gradient: .init(colors: [.blue,radius: 5).animation(.interactiveSpring())
}
}
struct DropDown4: View {
@State var expand = false
@Observedobject var feeling4 = SaveSettings()
var body: some View {
vstack() {
vstack(spacing: 30) {
HStack() {
Text("I am feeling: ").bold().foregroundColor(.white)
Text(feeling4.feeling4).bold().foregroundColor(.white)
Image(systemName: expand ? "chevron.up" : "chevron.down").resizable().frame(width: 13,height:6)
}.onTapGesture {
self.expand.toggle()
}
if expand {
Button(action: {
self.feeling4.feeling4 = "Happy"
self.expand.toggle()
}) {
Text("Happy")
}.foregroundColor(.white)
Button(action: {
self.feeling4.feeling4 = "Frustrated"
self.expand.toggle()
}) {
Text("Frustrated")
}.foregroundColor(.white)
Button(action: {
self.feeling4.feeling4 = "disappointed"
self.expand.toggle()
}) {
Text("disappointed")
}.foregroundColor(.white)
Button(action: {
self.feeling4.feeling4 = "Excited"
self.expand.toggle()
}) {
Text("Excited")
}.foregroundColor(.white)
Button(action: {
self.feeling4.feeling4 = "Amused"
self.expand.toggle()
}) {
Text("Amused")
}.foregroundColor(.white)
Button(action: {
self.feeling4.feeling4 = "AnxIoUs"
self.expand.toggle()
}) {
Text("AnxIoUs")
}.foregroundColor(.white)
Button(action: {
self.feeling4.feeling4 = "Confused"
self.expand.toggle()
}) {
Text("Confused")
}.foregroundColor(.white)
Button(action: {
self.feeling4.feeling4 = "Admiration"
self.expand.toggle()
}) {
Text("Admiration")
}.foregroundColor(.white)
Button(action: {
self.feeling4.feeling4 = "Embarassed"
self.expand.toggle()
}) {
Text("Embarrased")
}.foregroundColor(.white)
Group {
Button(action: {
self.feeling4.feeling4 = "Nostalgic"
self.expand.toggle()
}) {
Text("Nostalgic")
}.foregroundColor(.white)
Button(action: {
self.feeling4.feeling4 = "Surprised"
self.expand.toggle()
}) {
Text("Surprised")
}.foregroundColor(.white)
}
}
}
}.padding()
.background(LinearGradient(gradient: .init(colors: [.blue,radius: 5).animation(.interactiveSpring())
}
}
struct MultiTextField: UIViewRepresentable {
func makeCoordinator() -> Coordinator {
return MultiTextField.Coordinator(parent1: self)
}
@EnvironmentObject var obj : observed
func makeUIView(context: UIViewRepresentableContext<MultiTextField>) -> UITextView {
let view = UITextView()
view.font = .systemFont(ofSize: 16)
view.text = " "
view.textColor = UIColor.black.withAlphaComponent(0.350)
view.backgroundColor = .clear
view.delegate = context.coordinator
self.obj.size = view.contentSize.height
view.isEditable = true
view.isUserInteractionEnabled = true
view.isScrollEnabled = true
view.isSelectable = true
return view
}
func updateUIView(_ uiView: UITextView,context: UIViewRepresentableContext<MultiTextField>) {
}
class Coordinator: NSObject,UITextViewDelegate {
var parent : MultiTextField
init(parent1 : MultiTextField) {
parent = parent1
}
func textViewDidBeginEditing(_ textView: UITextView) {
textView.textColor = .black
}
func textViewDidChange(_ textView: UITextView) {
self.parent.obj.size = textView.contentSize.height
UserDefaults.standard.set(textView.text,forKey: "TextViewTextToRestore") //added
}
func textViewDidEndEditing(_ textView: UITextView) {
print("Editing is done")
}
}
}
解决方法
1。理想的方法:
您可能需要玩UIViewController
的状态恢复。请尝试https://www.raywenderlich.com/1395-state-restoration-tutorial-getting-started。
2。快速解决方案:
只要func textViewDidChange(_ textView: UITextView)
中的文本字段值发生变化,就尝试将值保存在UserDefaults
中。 [仅非敏感数据]
func textViewDidChange(_ textView: UITextView) {
self.parent.obj.size = textView.contentSize.height
// Save field value in Defaults
UserDefaults.standard.set(textView.text,forKey: "TextViewTextToRestore")
UserDefaults.standard..synchronize()
}
现在您可以恢复viewDidLoad()
中的值。
override func viewDidload() {
super.viewDidLoad()
if txtFileText = UserDefaults.standard.string(forKey: "TextViewTextToRestore") {
textView.text = txtFileText
}
}
注意:如果用户正常退出屏幕或根据您的使用情况,不要忘记从默认值中清除值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。