SwiftUI - 使用按钮和条件语句进行文本字段验证

SwiftUI - Textfield Validation with button and Conditional Statement

我想通过按下按钮来验证文本字段的输入。 当用户在文本字段中输入特定单词(例如:Predator)时,它应该触发 TextfieldVal 布尔值,并在按下“发送”按钮时将其设置为 true。除了 predator 之外,文本字段中的任何其他内容都应该是错误的。

现在我已经在互联网上搜索了几个小时,但仍然找不到我的答案。 问过我周围的一些人,但他们也帮不了我。 能否请你把我送到正确的方向。

这是我到目前为止想出的代码。 bool 有效,但我想念让它按预期工作的功能。因此,验证文本字段并使特定单词 return 为真,其他为假。


        @State var Textfield: String = ""
        @State var Answer: String = ""
        @State var ShowButton: Bool = false
        @State var TextFieldVal: Bool = false
       

        var body: some View {

                    VStack{
                        Text(Answer)
                            .frame(width: 400, height: 40, alignment: .center)
                            .font(.title)
                            .foregroundColor(Color.black)
                    
                    
                    TextField("Type here you answer...", text: $Textfield)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .frame(width: 250, height: 40, alignment: .center)
                        .background(Color.gray.opacity(0.5).cornerRadius(20))
                        .foregroundColor(.red)
                        .font(.headline)
                        
                        
                        Button {
                            
                            if TextFieldVal == true {
                                ShowButton = true
                                Answer = String ("That is Correct!")
                            } else {
                                Answer = ("That is not correct")
                            }
                                            
                                
                        } label: {
                            Text("Send")
                                .frame(width: 250, height: 40)
                                .background(Color(red: 0.272, green: 0.471, blue: 0.262))
                                .cornerRadius(20)
                                .foregroundColor(.white)
                                .font(.headline)
                            
                            if ShowButton {
                                NavigationLink(
                                    destination: Finish(),
                                
                                    label: {
                                        Text("Next")
                                            .frame(width: 120, height: 40)
                                            .background(Color.red)
                                            .cornerRadius(20)
                                            .shadow(radius: 10)
                                            .overlay(
                                                Text("Verder")
                                                    .foregroundColor(.white)
                                    
             
                            )}
                                    )}
                                }
                        
                        }
            

                    }
                    
                    }
                  

使用 onChange 修饰符。示例:


 @State var Textfield: String = ""
    @State var Answer: String = "Predator"
    @State var ShowButton: Bool = false
    @State var TextFieldVal: Bool = false
    
    
    var body: some View {
        
        VStack{
            Text(Answer)
                .frame(width: 400, height: 40, alignment: .center)
                .font(.title)
                .foregroundColor(Color.black)
            
            
            TextField("Type here you answer...", text: $Textfield)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .frame(width: 250, height: 40, alignment: .center)
                .background(Color.gray.opacity(0.5).cornerRadius(20))
                .foregroundColor(.red)
                .font(.headline)
            
            
            Button {
                
                if TextFieldVal == true {
                    ShowButton = true
                    Answer = "That is Correct!"
                } else {
                    
                    Answer = "That is not correct"
                }
                
                
            } label: {
                Text("Send")
                    .frame(width: 250, height: 40)
                    .background(Color(red: 0.272, green: 0.471, blue: 0.262))
                    .cornerRadius(20)
                    .foregroundColor(.white)
                    .font(.headline)
                
                if ShowButton {
                    NavigationLink(
                        destination: Example1(),
                        
                        label: {
                            Text("Next")
                                .frame(width: 120, height: 40)
                                .background(Color.red)
                                .cornerRadius(20)
                                .shadow(radius: 10)
                                .overlay(
                                    Text("Verder")
                                        .foregroundColor(.white)
                                    
                                    
                                )}
                    )}
            }
            
        }
        .onChange(of: Textfield) { _ in
            if Textfield == "Predator" {
                TextFieldVal = true
            } else {
                TextFieldVal = false
            }
        }
        
        
    }