如何在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
   }
}

注意:如果用户正常退出屏幕或根据您的使用情况,不要忘记从默认值中清除值。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...