在SwiftUI中为TextField设置初始值–比较新旧值。

我看到了很多关于如何使用空TextField来收集新的值的例子和教程,但是没有一个例子显示如何使用TextField来编辑一个值。

在我的使用案例中,我想让TextField从我的viewmodel中预先填充数据,然后当用户编辑数据时,应该启用一个保存按钮。在我的表单中,我也有一个导航链接,导致一个子页面,用户可以从列表中选择一些东西,然后被路由回表单。

只要我使用一个空字段,它的表现就像描述的那样;用户可以在字段中输入一些临时的东西,然后导航到子页面,临时值仍然像他离开时一样。

struct TextFieldDemo: View {

    var model:String    // Actual a more complex view model
    @State var editedValue:String = ""

    var body: some View {
        VStack(alignment: .leading, spacing: 20) {
            Group{
                Text("Some label")
                TextField("Placeholder text", text: $editedValue)
            }
            Divider()
            Text("Some navigation link to push in a page where " +
                "the user can select something from a list and click back...")

            // If the user starts to edit the textfield - follows a navigation link and comes back
            // he should be able to continue edit the field where he left of - the text field should
            // not have been reset to the original value.

            Button(action: {
                // Call some save function in the ViewModel
                },label: {
                    Text("SAVE")
                }
            ).disabled(model == editedValue)
        }.onAppear(){
            // I could have done something like:
            //   self.editedValue = model
            // but it seems like this will fire if the user navigates into the described page and reset
            // the TextField to the model value.
        }
    }
}

struct TextFieldDemo_Previews: PreviewProvider {
    static var previews: some View {
        TextFieldDemo(model: "The old value")
    }
}

解决方案:

要用模型中的值初始化文本字段,你需要定义你自己的初始化器并使用 State(wrappedValue:) 的初始化器 @State vars。

struct TextFieldDemo: View {

    var model:String    // Actual a more complex view model
    @State var editedValue: String

    init(model: String) {
        self.model = model
        self._editedValue = State(wrappedValue: model) // _editedValue is State<String>
    }

    var body: some View {
        VStack(alignment: .leading, spacing: 20) {
            Group{
                Text("Some label")
                TextField("Placeholder text", text: $editedValue)
            }
            Divider()
            Text("Some navigation link to push in a page where " +
                "the user can select something from a list and click back...")

            // If the user starts to edit the textfield - follows a navigation link and comes back
            // he should be able to continue edit the field where he left of - the text field should
            // not have been reset to the original value.

            Button(action: {
                // Call some save function in the ViewModel
                },label: {
                    Text("SAVE")
                }
            ).disabled(model == editedValue)
        }.onAppear(){
            // I could have done something like:
            //   self.editedValue = model
            // but it seems like this will fire if the user navigates into the described page and reset
            // the TextField to the model value.
        }
    }
}

struct TextFieldDemo_Previews: PreviewProvider {
    static var previews: some View {
        TextFieldDemo(model: "The old value")
    }
}

给TA打赏
共{{data.count}}人
人已打赏
未分类

如何构造一个可以抛出异常的对象?

2022-9-8 10:49:37

未分类

我如何在<select> html标签中的displayselected选项前加上一个字符串?[关闭]

2022-9-8 10:49:39

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索