0

一旦我的应用程序被双击/强制退出,如何在 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, leading: 25, bottom: 33, 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, 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))
                            
                            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, leading: 25, bottom: 33, 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, .purple]), startPoint: .top, endPoint: .bottom)).cornerRadius(15).shadow(color: .gray, 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, .purple]), startPoint: .top, endPoint: .bottom)).cornerRadius(15).shadow(color: .gray, 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, .purple]), startPoint: .top, endPoint: .bottom)).cornerRadius(15).shadow(color: .gray, 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")
        }  
    }
}
                  
4

1 回答 1

0

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
   }
}

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

于 2020-08-13T19:40:49.530 回答