我正在尝试构建一个记事卡应用程序,目前我正在用户可以输入记事卡的屏幕上工作。一切正常,除了当我为一张记事卡输入术语和定义时,它会更新所有其他记事卡,使它们具有相同的术语和定义。非常感谢您的帮助,不胜感激!:)
import SwiftUI
struct Notecard: Identifiable
{
let id = UUID()
let term2: String
let def2: String
}
class Notecards: ObservableObject
{
@Published var Notecardsarray = [Notecard]() //stores an array of the notecard items into a single object
}
struct ContentView: View{
@ObservedObject var notecardobject = Notecards()
@State private var term = "dfs"
@State private var def = "df"
var body: some View {
NavigationView{
List{
ForEach(notecardobject.Notecardsarray){item in
HStack{
TextField("enter term", text: self.$term)
TextField("enter definition", text: self.$def)
}
}
.onDelete(perform: removeItems)
}
.navigationBarTitle("Notecards")
.navigationBarItems(trailing:
Button(action: {
let newnotecard = Notecard(term2: self.term, def2: self.def)
self.notecardobject.Notecardsarray.append(newnotecard)
}) {
Image(systemName: "plus")
}
)
}
}
func removeItems(at offsets: IndexSet) {
notecardobject.Notecardsarray.remove(atOffsets: offsets)
}
}
//this will not actually be part of the app that goes to app store
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}