Swiftui-从外部设置了Textfield的焦点

发布于 2025-02-13 05:05:21 字数 1731 浏览 0 评论 0原文

我创建了一个自定义textfield来显示和编辑我的应用程序中的项目标题。当用户创建一个新项目时,我想将此textfield自动.onappear自动关注,因此可以立即输入标题。对于现有项目,TextField不应集中精力。

这样做的唯一方法是在codexfield之外保存@State,如下示例吗?对我来说,似乎有一种更优雅的方法可以实现这一目标,而没有创建另一个@State

这是一些简化的示例代码:

struct ContentView: View {
    @State var text: String = "Text"
    @State var focusOnAppear = false
    @State var itemID = ""

    var body: some View {

        VStack {
            Button("+ New item") {
                focusOnAppear = true
                itemID = "0"
            }
            Button("Existing item"){
                focusOnAppear = false
                itemID = "1"
            }
            Button("Another existing item"){
                focusOnAppear = false
                itemID = "2"
            }
        }

        CustomTextField($text, focusOnAppear: focusOnAppear)
            .frame(width: 200, height: 44)
            .id(itemID)
    }
}

struct CustomTextField: View {

    @Binding private var text: String
    @FocusState var isFocused: Bool

    var focusOnAppear = false

    init(_ text: Binding<String>, focusOnAppear: Bool) {
        self._text = text
        self.focusOnAppear = focusOnAppear
    }

    var body: some View {
        TextField("", text: $text)
            .focused($isFocused)
            .foregroundColor(isFocused ? .red : .primary)
            .border(Color.gray)
            .onAppear {
                if focusOnAppear {
                    DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
                        isFocused = true
                    }
                }
            }
    }
}

I have created a custom TextField to display and edit the title of items in my app. When a user creates a new item I would like to focus this TextField automatically .onAppear so the title can be entered immediately. For existing items the TextField should not be focused.

Is the only way to do this to hold a @State outside of the CustomTextField like in the example below? To me it seems there might be a more elegant way to achieve this without creating yet another @State.

Here's a bit of simplified example code:

struct ContentView: View {
    @State var text: String = "Text"
    @State var focusOnAppear = false
    @State var itemID = ""

    var body: some View {

        VStack {
            Button("+ New item") {
                focusOnAppear = true
                itemID = "0"
            }
            Button("Existing item"){
                focusOnAppear = false
                itemID = "1"
            }
            Button("Another existing item"){
                focusOnAppear = false
                itemID = "2"
            }
        }

        CustomTextField($text, focusOnAppear: focusOnAppear)
            .frame(width: 200, height: 44)
            .id(itemID)
    }
}

struct CustomTextField: View {

    @Binding private var text: String
    @FocusState var isFocused: Bool

    var focusOnAppear = false

    init(_ text: Binding<String>, focusOnAppear: Bool) {
        self._text = text
        self.focusOnAppear = focusOnAppear
    }

    var body: some View {
        TextField("", text: $text)
            .focused($isFocused)
            .foregroundColor(isFocused ? .red : .primary)
            .border(Color.gray)
            .onAppear {
                if focusOnAppear {
                    DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
                        isFocused = true
                    }
                }
            }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文