Swiftui 读取@Binding值

Swiftui 读取@Binding值,swiftui,textfield,Swiftui,Textfield,我尝试自定义textfield,所以我给它@Binding以响应如下所示的文本值,问题是当我尝试检测文本的变化时,它只是在“预览”中响应,但当在“模拟器”上运行应用程序时,它没有响应,我尝试了许多不同的方法来解决这个问题,但没有任何效果 import SwiftUI struct MyTextField: UIViewRepresentable { typealias UIViewType = UITextField @Binding var becomeF

我尝试自定义textfield,所以我给它@Binding以响应如下所示的文本值,问题是当我尝试检测文本的变化时,它只是在“预览”中响应,但当在“模拟器”上运行应用程序时,它没有响应,我尝试了许多不同的方法来解决这个问题,但没有任何效果

import SwiftUI

struct MyTextField: UIViewRepresentable {
    
    typealias UIViewType = UITextField
    
    @Binding var becomeFirstResponder: Bool
    @Binding var text: String
    var placeholder = ""
    
    func makeUIView(context: Context) -> UITextField {
        let textField = UITextField()
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.widthAnchor.constraint(equalToConstant: 320).isActive = true
        
        textField.textColor = UIColor.systemBlue
        textField.font = UIFont.boldSystemFont(ofSize: 22)
        
        textField.textAlignment = .left
        textField.keyboardType = .default
        
        textField.minimumFontSize = 13
        textField.adjustsFontSizeToFitWidth = true
        
        textField.text = self._text.wrappedValue
        textField.placeholder = self.placeholder
        textField.delegate = context.coordinator
        
        return textField
    }
    
    func updateUIView(_ textField: UITextField, context: Context) {
        if self.becomeFirstResponder {
            DispatchQueue.main.async {
                textField.becomeFirstResponder()
                self.becomeFirstResponder = false
            }
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(parent: self)
    }
    
    class Coordinator: NSObject, UITextFieldDelegate {
        var parent: MyTextField
        
        init(parent: MyTextField) {
            self.parent = parent
        }
        
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            textField.resignFirstResponder()
        }
        
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            
            let currentText = textField.text ?? ""
            
            guard let stringRange = Range(range, in: currentText) else {
                return false
            }
            
            let updateText = currentText.replacingCharacters(in: stringRange, with: string)
            
            return updateText.count < 20
            
        }
    }
}



struct TextFieldFirstResponder: View {
    
    @State private var becomeFirstResponder = false
    @State private var text = "LLL"
    private var placeholder = "Untitled"
    
    var body: some View {
        
        VStack {
            ZStack(alignment: .trailing) {
                MyTextField(becomeFirstResponder: self.$becomeFirstResponder, text: self.$text, placeholder: self.placeholder)
                    .frame(width: 343, height: 56, alignment: .leading)
                    .padding(EdgeInsets(top: 27, leading: 13, bottom: 0, trailing: 0))
                    .background(
                        RoundedRectangle(cornerRadius: 10, style: .continuous)
                            .fill(Color(UIColor.secondarySystemBackground))
                            .frame(width: 342, height: 56, alignment: .center)
                    )
                    .onAppear {
                        self.becomeFirstResponder = true
                    }
            }
            Text("\(self.$text.wrappedValue)") // <------ Do not read the "text"
        }
    }
}

struct TextFieldFirstResponder_Previews: PreviewProvider {
    static var previews: some View {
        TextFieldFirstResponder()
    }
}
导入快捷界面
结构MyTextField:UIViewRepresentable{
typealias UIViewType=UITextField
@绑定变量成为第一响应者:Bool
@绑定变量文本:字符串
var placeholder=“”
func makeUIView(上下文:context)->UITextField{
设textField=UITextField()
textField.translatesAutoResizezingMaskintoConstraints=false
textField.widthAnchor.constraint(equalToConstant:320).isActive=true
textField.textColor=UIColor.systemBlue
textField.font=UIFont.boldSystemFont(字体大小:22)
textField.textAlignment=.left
textField.keyboardType=.default
textField.minimumFontSize=13
textField.adjustsFontSizeToFitWidth=true
textField.text=self.\u text.wrappedValue
textField.placeholder=self.placeholder
textField.delegate=context.coordinator
返回文本字段
}
func updateUIView(textField:UITextField,context:context){
如果自我成为第一反应者{
DispatchQueue.main.async{
textField.becomeFirstResponder()
self.becomeFirstResponder=false
}
}
}
func makeCoordinator()->Coordinator{
协调员(家长:自我)
}
类协调器:NSObject,UITextFieldDelegate{
var父项:MyTextField
初始化(父项:MyTextField){
self.parent=parent
}
func textField应该返回(textField:UITextField)->Bool{
textField.resignFirstResponder()辞职
}
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
让currentText=textField.text??“”
guard let stringRange=范围(范围,in:currentText)else{
返回错误
}
让updateText=currentText.replacingCharacters(在:stringRange中,带:string)
返回updateText.count<20
}
}
}
结构TextFieldFirstResponder:视图{
@国家私有变量becomeFirstResponder=false
@国家私有var text=“LLL”
私有变量占位符=“无标题”
var body:一些观点{
VStack{
ZStack(对齐:。尾部){
MyTextField(becomeFirstResponder:self.$becomeFirstResponder,text:self.$text,占位符:self.placeholder)
.框架(宽度:343,高度:56,对齐:。前导)
.填充(边集(顶部:27,前导:13,底部:0,尾随:0))
.背景(
圆角转角(拐角半径:10,样式:连续)
.fill(颜色(UIColor.secondary系统背景))
.框架(宽度:342,高度:56,对齐:。中心)
)
奥纳佩尔先生{
self.becomeFirstResponder=true
}
}

Text(\(self.$Text.wrappedValue)/您不需要在此处使用
$
直接读取属性

Text(self.text) // <------ Do not use $
Text(self.Text)//
let updateText = currentText.replacingCharacters(in: stringRange, with: string)
self.parent.text = updateText          // << here !!
return updateText.count < 20