【问题标题】:SwiftUI TextField CoreData - Changing an attribute's dataSwiftUI TextField CoreData - 更改属性的数据
【发布时间】:2019-07-21 17:57:14
【问题描述】:

我正在尝试使用 TextField 来更改 CoreData 属性的数据,但我想出的一切都没有成功。有一个类似的问题(如下所列),我将把正确答案的代码贴出来解释一下。

struct ItemDetail: View {
    @EnvironmentObject var itemStore: ItemStore
    let idx: Int


    var body: some View {
        NavigationView {
            Stepper(value: $itemStore.items[idx].inventory) {
                Text("Inventory is \(self.itemStore.items[idx].inventory)")
            }

                // Here I would like to do this
                // TextField("PlaceHolder", $itemStore.items[idx].name)

                // That doesn't work... also tried
                // TextField("PlaceHolder", $name) - where name is a @State String
                // How can you then automaticlly assign the new value of @State name
                // To $itemStore.items[idx].name?


            .padding()
            .navigationBarTitle(itemStore.items[idx].name)
        }
    }
}

原问题:

SwiftUI @Binding doesn't refresh View

【问题讨论】:

    标签: core-data swiftui


    【解决方案1】:

    我现在可以正常使用了。

    struct ItemDetail: View {
    @EnvironmentObject var itemStore: ItemStore
    let idx: Int
    
    // Added new @State variable
    @State var name = ""
    
    
    var body: some View {
        NavigationView {
            Stepper(value: $itemStore.items[idx].inventory) {
                Text("Inventory is \(self.itemStore.items[idx].inventory)")
            }
    
            TextField("Placeholder", text: $name) {
                // When the enter key is tapped, this runs.
                self.itemStore.items[self.idx].name = self.name
            }
    
            .padding()
            .navigationBarTitle(itemStore.items[idx].name)
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2020-04-12
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多