首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI:如何在不隐藏键盘的情况下移动/跳转到下一个TextField?

问SwiftUI:如何在不隐藏键盘的情况下移动/跳转到下一个TextField?
EN

Stack Overflow用户
提问于 2022-07-25 11:43:03
回答 1查看 355关注 0票数 1

我正在解决如何在纯SwiftUI中移动/跳转。下面是不正确工作的示例代码,但是有一个应该如何工作的概念。

代码语言:javascript
复制
struct ContentView: View {
    @FocusState private var focused: Bool
    @State private var activeIndex: Int = 0

    var body: some View {
        HStack(spacing: 3) {
            ForEach(0..<10, id: \.self) { index in
                Stone(activeIndex: $activeIndex, focused: _focused, index: index)
            }
        }
                .padding(.horizontal)
    }
}

当我试图移动/跳转到下一个TextField时,问题是什么都不会发生。

代码语言:javascript
复制
struct Stone: View {
    @Binding var activeIndex: Int
    @FocusState var focused: Bool
    @State var index: Int
    @State private var text: String = ""

    var body: some View {
        GeometryReader { bounds in
            ZStack {
                TextField("", text: $text)
                        .multilineTextAlignment(.center)
                        .focused($focused)
                        .background(index == activeIndex ? .red : .green)
                        .onChange(of: text) { newValue in
                            if let lastValue = newValue.last {
                                text = String(lastValue)

                                activeIndex += 1

                                focused = true
                            }
                        }
            }
                    .frame(width: bounds.size.width, height: bounds.size.height)
        }
                .scaledToFit()
    }
}

我发现了一些如何跳转/移动到下一个TextField的方法,但主要是在UIKit中。因此,我的问题是,如何在纯TextField中移动/跳转到下一个SwiftUI?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-27 16:52:37

这里有一个函数例子来理解我的问题是什么意思。

代码语言:javascript
复制
struct ContentView: View {
    @FocusState private var focusedField: Int?

    var body: some View {
        HStack(spacing: 3) {
            ForEach(0..<10, id: \.self) { index in
                Stone(focusedField: _focusedField, index: index)
            }
        }
                .padding(.horizontal)
    }
}
代码语言:javascript
复制
struct Stone: View {
    @FocusState var focusedField: Int?
    @State var index: Int
    @State private var text: String = ""

    var body: some View {
        TextField("", text: $text)
                .focused($focusedField, equals: index)
                .textFieldStyle(.roundedBorder)
                .onChange(of: text) { newValue in
                    setFocus(index: index)
                }
    }

    func setFocus(index: Int) {
        /// Default to the first box when focus is not set or the user reaches the last box
        if focusedField == nil || focusedField == 9 {
            focusedField = 0
        } else {
            /// Another safety check for the index
            if index == 9 {
                focusedField = 0
            } else {
                focusedField = index + 1
            }
        }
    }
}

感谢大家!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73108663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档