繁体   English   中英

SwiftUI - 带有按钮和条件语句的文本字段验证

[英]SwiftUI - Textfield Validation with button and Conditional Statement

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

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

这是我到目前为止想出的代码。 bool 有效,但我想念使其按预期工作的功能。 因此验证文本字段并使特定单词返回 true,其他任何返回 false。


        @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
            }
        }
        
        
    }
    



暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM