SwiftUI列表中的可编辑TextField [英] Editable TextField in SwiftUI List

查看:63
本文介绍了SwiftUI列表中的可编辑TextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:14个月后,AppKit发行说明中有一个有趣的说明:

UPDATE: 14 months later, there is this intriguing note in the AppKit release notes:

您正在选定列表行内编辑的TextField现在具有正确的文本前景色.(68545878)

A TextField that you are editing inside a selected List row now has correct text foreground colors. (68545878)

现在,当将TextField放置在列表中时,TextField第一次被选中时会集中在单击上,但是随后的编辑尝试失败:选择了List行,但TextField没有获得焦点.

Now when placing a TextField in a List, the TextField becomes focused on click the very first time it is selected, but subsequent editing attempts fail: the List row is selected but the TextField does not gain focus.

O/P:

在beta6版 SwiftUI macOS(不是 iOS)应用中,我需要具有带有可编辑文本字段的 List ,但是列表中的> TextField 不可编辑.如果我将 List 换为 Form (或者只是 VStack ),它会很好用,但是我需要它与 List 代码>.有什么技巧告诉列表使字段可编辑吗?

In a beta6 SwiftUI macOS (not iOS) app, I need to have a List with editable text fields, but the TextFields in the list are not editable. It works fine if I swap out List for Form (or just VStack), but I need it working with a List. Is there some trick to tell the list to make the fields editable?

import SwiftUI

struct ContentView: View {
    @State var stringField: String = ""

    var body: some View {
        List { // works if this is VStack or Form
            Section(header: Text("Form Header"), footer: Text("Form Footer")) {
                TextField("Line 1", text: $stringField).environment(\.isEnabled, true)
                TextField("Line 2", text: $stringField)
                TextField("Line 3", text: $stringField)
                TextField("Line 4", text: $stringField)
                TextField("Line 5", text: $stringField)
            }
        }
    }
}

推荐答案

错误报告

我更新了该项目,以MacOS应用为目标,并找到了您要报告的错误.我已经向Apple更新了此反馈,因为它确实确实是个错误(欢迎使用Beta).

Bug Report

I updated the project to target a MacOS app and found the bug you are reporting. I've updated Apple with this feedback because it indeed does seem to be a bug (Welcome to Beta).

FB7174245-嵌入在列表中的SwiftUI文本字段不可编辑当目标是macOS时

FB7174245 - SwiftUI Textfields embedded in a List are not editable when target is macOS

更新

那么下面所有关注状态和绑定的意义何在?一个变量应绑定到单个控件.这是一个工作示例.我将保留较旧的答案,因为它通过完整的应用程序来保存和从CoreData检索数据以及从CoreData检索数据的方式继续进行示例.

Update

So what's the point of all the focus on state and binding below? One variable should be bound to a single control. Here is a working example. I'll leave up the older answer as it carries the example forward with a full app saving and retrieving data to/from CoreData.

import SwiftUI

struct ContentView: View {
    @State var field1: String = ""
    @State var field2: String = ""
    @State var field3: String = ""
    @State var field4: String = ""
    @State var field5: String = ""

    var body: some View {
        List { // works if this is VStack or Form
            Section(header: Text("Form Header"), footer: Text("Form Footer")) {
                TextField("Line 1", text: $field1).environment(\.isEnabled, true)
                TextField("Line 2", text: $field2)
                TextField("Line 3", text: $field3)
                TextField("Line 4", text: $field4)
                TextField("Line 5", text: $field5)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

BTW-可以在MacOS Catalina上的Xcode Beta(11M392r)上使用-10.15Beta(19A546d).

BTW - This works on Xcode Beta (11M392r) with MacOS Catalina - 10.15 Beta (19A546d).

示例项目

查看此示例,其中包括一个可编辑的Textfield,它写入CoreData,我是在Github上构建.

Sample Project

Check out this sample that includes an editable Textfield that writes to CoreData, which I am building on Github.

看看@State和@Binding之间的区别,以便您可以从内容视图外部操作数据.( 60秒视频)

struct ContentView: View {
    // 1.
    @State var name: String = ""
    var body: some View {
        VStack {
            // 2.
            TextField(" Enter some text", text: $name)
                .border(Color.black)
            Text("Text entered:")
            // 3.
            Text("\(name)")
        }
        .padding()
        .font(.title)
    }
}

查看以下答案,以了解@State的适当用例( SO答案)

这篇关于SwiftUI列表中的可编辑TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆