我要创建一个登录代码屏幕。它由4个单独的UITextField
元素组成,每个元素都接受一个字符。我所做的是实现一个系统,每次UITextField
的更改之一时,它都会验证所有值是否都已填写,以及是否更新了布尔绑定以告诉父对象代码正确
为此,我将@State
变量包装在一个自定义绑定中,该绑定对设置器进行回调,如下所示:
@State private var chars:[String] = ["","","",""]
...
var body: some View {
var bindings:[Binding<String>] = []
for x in 0..<self.chars.count {
let b = Binding<String>(get: {
return self.chars[x]
}, set: {
self.chars[x] = $0
self.validateCode()
})
bindings.append(b)
}
并将那些绑定传递给组件。每次我的文本值更改validateCode
时,都会调用一次。效果很好。
但是,现在我想添加一个额外的行为:如果用户键入4个字符并且代码错误,我想将第一个响应者移回第一个文本字段并清除其内容。第一个响应者部分工作正常(我也使用@State
变量来管理它,但是我没有为它们使用绑定包装器),但是我无法在代码中更改文本。我认为这是因为我的组件使用的是包装的绑定,而不是包含文本的变量。
这是我的validateCode
的样子:
func validateCode() {
let combinedCode = chars.reduce("") { (result, string) -> String in
return result + string
}
self.isValid = value == combinedCode
if !isValid && combinedCode.count == chars.count {
self.hasFocus = [true,false,false,false]
self.chars = ["","","",""]
}
}
hasFocus
正确执行其操作,并且光标正在移动到第一个UITextField
。但是,文本保留在文本字段中。我尝试在init
中创建这些绑定,因此也可以在validateCode
函数中使用它们,但是由于在getter和setter中使用self
,因此会产生各种编译错误。
有什么办法解决这个问题吗?我应该使用Observables吗?我只是从SwiftUI开始,所以可能我缺少一些可用于此目的的工具。
为完整起见,以下是整个文件的代码:
import SwiftUI
struct CWCodeView: View {
var value:String
@Binding var isValid:Bool
@State private var chars:[String] = ["","","",""]
@State private var hasFocus = [true,false,false,false]
@State private var nothingHasFocus:Bool = false
init(value:String,isValid:Binding<Bool>) {
self.value = value
self._isValid = isValid
}
func validateCode() {
let combinedCode = chars.reduce("") { (result, string) -> String in
return result + string
}
self.isValid = value == combinedCode
if !isValid && combinedCode.count == chars.count {
self.hasFocus = [true,false,false,false]
self.nothingHasFocus = false
self.chars = ["","","",""]
}
}
var body: some View {
var bindings:[Binding<String>] = []
for x in 0..<self.chars.count {
let b = Binding<String>(get: {
return self.chars[x]
}, set: {
self.chars[x] = $0
self.validateCode()
})
bindings.append(b)
}
return GeometryReader { geometry in
ScrollView (.vertical){
VStack{
HStack {
CWNumberField(letter: bindings[0],hasFocus: self.$hasFocus[0], previousHasFocus: self.$nothingHasFocus, nextHasFocus: self.$hasFocus[1])
CWNumberField(letter: bindings[1],hasFocus: self.$hasFocus[1], previousHasFocus: self.$hasFocus[0], nextHasFocus: self.$hasFocus[2])
CWNumberField(letter: bindings[2],hasFocus: self.$hasFocus[2], previousHasFocus: self.$hasFocus[1], nextHasFocus: self.$hasFocus[3])
CWNumberField(letter: bindings[3],hasFocus: self.$hasFocus[3], previousHasFocus: self.$hasFocus[2], nextHasFocus: self.$nothingHasFocus)
}
}
.frame(width: geometry.size.width)
.frame(height: geometry.size.height)
.modifier(AdaptsToSoftwareKeyboard())
}
}
}
}
struct CWCodeView_Previews: PreviewProvider {
static var previews: some View {
CWCodeView(value: "1000", isValid: .constant(false))
}
}
struct CWNumberField : View {
@Binding var letter:String
@Binding var hasFocus:Bool
@Binding var previousHasFocus:Bool
@Binding var nextHasFocus:Bool
var body: some View {
CWSingleCharacterTextField(character:$letter,hasFocus: $hasFocus, previousHasFocus: $previousHasFocus, nextHasFocus: $nextHasFocus)
.frame(width: 46,height:56)
.keyboardType(.numberPad)
.overlay(
RoundedRectangle(cornerRadius: 5)
.stroke(Color.init("codeBorder"), lineWidth: 1)
)
}
}
struct CWSingleCharacterTextField : UIViewRepresentable {
@Binding var character: String
@Binding var hasFocus:Bool
@Binding var previousHasFocus:Bool
@Binding var nextHasFocus:Bool
func makeUIView(context: Context) -> UITextField {
let textField = UITextField.init()
//textField.isSecureTextEntry = true
textField.keyboardType = .numberPad
textField.delegate = context.coordinator
textField.textAlignment = .center
textField.font = UIFont.systemFont(ofSize: 16)
textField.tintColor = .black
textField.text = character
return textField
}
func updateUIView(_ uiView: UITextField, context: Context) {
if hasFocus {
DispatchQueue.main.async {
uiView.becomeFirstResponder()
}
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
class Coordinator : NSObject, UITextFieldDelegate {
var parent:CWSingleCharacterTextField
init(_ parent:CWSingleCharacterTextField) {
self.parent = parent
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let result = (textField.text! as NSString).replacingCharacters(in: range, with: string)
if result.count > 0 {
DispatchQueue.main.async{
self.parent.hasFocus = false
self.parent.nextHasFocus = true
}
} else {
DispatchQueue.main.async{
self.parent.hasFocus = false
self.parent.previousHasFocus = true
}
}
if result.count <= 1 {
parent.character = string
return true
}
return false
}
}
}
谢谢!
答案 0 :(得分:0)
您只是犯了一个小错误,但我不敢相信您只是“开始”了SwiftUI;)
1。)只是一次构建文本字段,所以我将其作为成员变量而不是始终构建新的变量 2.)更新updateuiview中的文本->就这样 3.)...几乎:仍然存在焦点/更新问题...四个文本字段中的最后一个无法正确更新...我认为这是焦点问题....
尝试一下:
struct CWSingleCharacterTextField : UIViewRepresentable {
@Binding var character: String
@Binding var hasFocus:Bool
@Binding var previousHasFocus:Bool
@Binding var nextHasFocus:Bool
let textField = UITextField.init()
func makeUIView(context: Context) -> UITextField {
//textField.isSecureTextEntry = true
textField.keyboardType = .numberPad
textField.delegate = context.coordinator
textField.textAlignment = .center
textField.font = UIFont.systemFont(ofSize: 16)
textField.tintColor = .black
textField.text = character
return textField
}
func updateUIView(_ uiView: UITextField, context: Context) {
uiView.text = character
if hasFocus {
DispatchQueue.main.async {
uiView.becomeFirstResponder()
}
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
class Coordinator : NSObject, UITextFieldDelegate {
var parent:CWSingleCharacterTextField
init(_ parent:CWSingleCharacterTextField) {
self.parent = parent
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let result = (textField.text! as NSString).replacingCharacters(in: range, with: string)
if result.count > 0 {
DispatchQueue.main.async{
self.parent.hasFocus = false
self.parent.nextHasFocus = true
}
} else {
DispatchQueue.main.async{
self.parent.hasFocus = false
self.parent.previousHasFocus = true
}
}
if result.count <= 1 {
parent.character = string
return true
}
return false
}
}
}