我有一个文本字段,用户可以在其中手动输入位置字符串.这与我所观察到的物体AppState
绑定,我重新标记为(app
),它有一个称为@Published var
的destination
.这工作得很好,我可以像这样在文本字段中绑定手动输入的文本:
TextField("Destination", text: $app.destination)
我已经开始try 实现一个Collection 夹系统,用户可以保存和更改多达4个目的地,以便快速输入,无需重新输入(一次只能在文本字段中输入1个).
当用户按下他们的最爱之一时,最爱就会出现在文本字段中,他们可以通过输入新的最爱并保存它来更改它,或者按下新的最爱来替换文本字段中的最爱.用户还必须可以 Select 根本不输入最喜欢的内容,并且还可以随机输入一个.
到目前为止,我的这个部分运行得相当好,并且对它的功能感到满意.
我这里的问题是,我不知道如何将文本字段中的内容绑定到我的ObservedObject
app.destination
.经过一些故障处理后,我发现只有手动输入的文本才会存储在app.destination
中.文本字段中出现最喜欢的内容不会绑定.
我try 过.onChange()
、.onTapGesture()
和.focused()
的变体来运行if/else块,该块将简单地将文本字段中的文本分配给app.destination
,但它仍然有缺陷,并且您无法保存新的最爱.
我正在绕圈子试图弄清楚这个问题,并非常感谢一些帮助.
我有一个最低可复制的示例供您复制和粘贴.谢谢你
主视图
struct ContentView: View {
@StateObject private var favoritesViewModel = FavoritesViewModel()
@ObservedObject var app = AppState()
@State private var newLocation = ""
@State private var selectedLocationIndex = 0
@State private var userSelectedFavourite: String = ""
@State private var favouritePressed: Bool = false
@FocusState private var isFocused: Bool
var body: some View {
VStack {
HStack {
//.........THIS IS THE ISSUE TEXTFIELD
TextField("Destination", text: (favouritePressed && !isFocused) ? $favoritesViewModel.favoriteLocations[selectedLocationIndex] : $newLocation)
//.....................................
.focused($isFocused)
.contentShape(.rect)
.textInputAutocapitalization(.characters)
.disableAutocorrection(true)
.padding()
.frame(width: 280, height: 60)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.gray, lineWidth: 2)
)
Button("Save") {
saveLocation(locationIndex: selectedLocationIndex)
}
}
Picker("Favorite Locations", selection: $selectedLocationIndex) {
ForEach(favoritesViewModel.favoriteLocations.indices, id: \.self) { index in
Text(favoritesViewModel.favoriteLocations[index])
.tag(index)
}
}
.onChange(of: selectedLocationIndex) {
favouritePressed = true
isFocused = false
}
.pickerStyle(.palette)
Spacer()
}
.navigationTitle("Favorite Locations")
}
func saveLocation(locationIndex: Int) {
print("Saving...")
if !newLocation.isEmpty {
favoritesViewModel.addFavoriteLocation(newLocation, locationIndex: locationIndex)
newLocation = ""
}
}
}
这个只存储最喜欢的:
class FavoritesViewModel: ObservableObject {
// Key for UserDefaults
let favoritesKey = "favoriteLocations"
// UserDefaults instance
var defaults: UserDefaults {
UserDefaults.standard
}
// Published property to manage favorite locations
@Published var favoriteLocations: [String] = []
// Initialize with default values
init() {
self.favoriteLocations = loadFavorites()
}
// Load favorite locations from UserDefaults
func loadFavorites() -> [String] {
return defaults.stringArray(forKey: favoritesKey) ?? []
}
// Save favorite locations to UserDefaults
func saveFavorites() {
defaults.set(favoriteLocations, forKey: favoritesKey)
}
// Add a new favorite location
func addFavoriteLocation(_ location: String, locationIndex: Int) {
// Check if location is already in favorites and limit to 4 locations
if !favoriteLocations.contains(location) {
if favoriteLocations.count < 4 {
favoriteLocations.insert(location, at: locationIndex)
} else {
favoriteLocations.remove(at: locationIndex)
favoriteLocations.insert(location, at: locationIndex)
}
saveFavorites() // Save after adding or updating favorites
}
}
}
仅观察类:
class AppState: ObservableObject {
// THIS IS THE VARIABLE IM TRYING TO SAVE TEXT TO
@Published var destination: String = ""
}