skip to Main Content

I have a view that contain users UsersContentView in this view there is a button which is extracted as a subview: RequestSearchButton(), and under the button there is a Text view which display the result if the user did request to search or no, and it is also extracted as a subview ResultSearchQuery().

struct UsersContentView: View {
    
    var body: some View {
        
        ZStack {
            VStack {
                RequestSearchButton()
                
                ResultSearchQuery(didUserRequestSearchOrNo: .constant("YES"))
            }
            
            
            
        }
        
    }
}


struct RequestSearchButton: View {
    var body: some View {
        Button(action: {
            
        }) {
            Text("User requested search")
        }
    }
}



struct ResultSearchQuery: View {
    @Binding var didUserRequestSearchOrNo: String
    var body: some View {
        Text("Did user request search: (didUserRequestSearchOrNo)")
    }
}

How can I update the @Binding var didUserRequestSearchOrNo: String inside the ResultSearchQuery() When the button RequestSearchButton() is clicked. Its so confusing!

2

Answers


  1. Actually I couldn’t understand why you used two struct which are connected to eachother, you can do it in one struct and Control with a state var

        struct ContentView: View {
        
        var body: some View {
    
                VStack {
                    
                    RequestSearchButton()
    
            }
            
        }
    }
    
    
    struct RequestSearchButton: View {
        
        @State private var clicked : Bool = false
        
        var body: some View {
            
            Button(action: {
                
                clicked = true
                
            }) {
                
                Text("User requested search")
                
            }
            
            Text("Did user request search: (clicked == true ? "YES" : "NO")")
            
        }
    }
    

    if this is not what you are looking for, could you make a detailed explain.

    Login or Signup to reply.
  2. You need to track the State of a variable (which is indicating if a search is active or not) in your parent view, or your ViewModel if you want to extract the Variables. Then you can refer to this variable in enclosed child views like the Search Button or Search Query Results.

    In this case a would prefer a Boolean value for the tracking because it’s easy to handle and clear in meaning.

    struct UsersContentView: View {
        
        @State var requestedSearch = false
        
        var body: some View {
            
            ZStack {
                VStack {
                    RequestSearchButton(requestedSearch: $requestedSearch)
                    
                    ResultSearchQuery(requestedSearch: $requestedSearch)
                }
                
                
                
            }
            
        }
    }
    
    struct RequestSearchButton: View {
        @Binding var requestedSearch: Bool
        var body: some View {
            Button(action: {
                requestedSearch.toggle()
            }) {
                Text("User requested search")
            }
        }
    }
    
    
    
    struct ResultSearchQuery: View {
        @Binding var requestedSearch: Bool
        var body: some View {
            Text("Did user request search: (requestedSearch.description)")
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search