[英]Storing a selected value from picker to use in other views - SwiftUI
I was very kindly helped to allow my picker to select a value from my Firestore database.我得到了非常友好的帮助,允许我的选择器从我的 Firestore 数据库中选择一个值。 What I would like to do is once that value is selected in my picker I would like to be able to show that value in other views.我想做的是一旦在我的选择器中选择了该值,我希望能够在其他视图中显示该值。 I have tried setting this up using UserDefaults but I'm not sure that's the way to go?我试过使用 UserDefaults 进行设置,但我不确定这是要走的路吗? If you could suggest a better method I'd be more than grateful.如果您能提出更好的方法,我将不胜感激。 My code currently is below.我的代码目前在下面。
The value in the below code returns Unknown School each time but without the user defaults works flawlessly in fetching the data.下面代码中的值每次都返回 Unknown School 但没有用户默认值在获取数据时完美无缺。
Thanks in advance.提前致谢。
import SwiftUI
import Firebase
struct SchoolDetailsView: View {
@ObservedObject var schoolData = getSchoolData()
@State private var selectedSchool = UserDefaults.standard.string(forKey: "") // `schoolName.id` is of type String
var body: some View {
VStack {
Form {
Section {
Picker(selection: $selectedSchool, label: Text("School Name")) {
ForEach(schoolData.datas, id: \.id) {
Text($0.name)
}
}
Text("Selected School: \(selectedSchool ?? "Unknown School")")
}
}.navigationBarTitle("School Selection")
}
}
struct SchoolPicker_Previews: PreviewProvider {
static var previews: some View {
SchoolDetailsView()
}
}
class getSchoolData : ObservableObject{
@Published var datas = [schoolName]()
init() {
let db = Firestore.firestore()
db.collection("School Name").addSnapshotListener { (snap, err) in
if err != nil{
print((err?.localizedDescription)!)
return
}
for i in snap!.documentChanges{
let id = i.document.documentID
let name = i.document.get("Name") as! String
self.datas.append(schoolName(id: id, name: name))
}
}
}
}
struct schoolName : Identifiable {
var id : String
var name : String
}
}
First, the UserDefaults key for your variable can't be empty:首先,变量的 UserDefaults 键不能为空:
@State private var selectedSchool: String = UserDefaults.standard.string(forKey: "selectedSchool") ?? "Unknown School"
Then you can use onReceive
to update the variable:然后你可以使用onReceive
来更新变量:
.onReceive(Just(selectedSchool)) {
UserDefaults.standard.set($0, forKey: "selectedSchool")
}
Full code:完整代码:
import Combine
import Firebase
import SwiftUI
struct SchoolDetailsView: View {
@ObservedObject var schoolData = getSchoolData()
@State private var selectedSchool = UserDefaults.standard.string(forKey: "selectedSchool")
var body: some View {
VStack {
Form {
Section {
Picker(selection: $selectedSchool, label: Text("School Name")) {
ForEach(schoolData.datas, id: \.id) {
Text($0.name)
}
}
.onReceive(Just(selectedSchool)) {
UserDefaults.standard.set($0, forKey: "selectedSchool")
}
Text("Selected School: \(selectedSchool)")
}
}.navigationBarTitle("School Selection")
}
}
}
Note that in SwiftUI 2 / iOS 14 you can use @AppStorage instead.请注意,在SwiftUI 2 / iOS 14 中,您可以改用@AppStorage 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.