iOS SwiftUI Searchbar and REST-API

前端 未结 2 1924
悲哀的现实
悲哀的现实 2021-02-04 22:14

I\'m experimenting with SwiftUI and would like to fetch an update from my REST API with a search string.

However, I\'m not sure how to bring the two components together

2条回答
  •  梦如初夏
    2021-02-04 22:49

    There is no need to get UIKit involved, you can declare a simple search bar like this:

    struct SearchBar: View {
    
        @State var searchString: String = ""
    
        var body: some View {
    
            HStack {
                TextField("Start typing",
                          text: $searchString,
                          onCommit: { self.performSearch() })
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                Button(action: { self.performSearch() }) {
                    Image(systemName: "magnifyingglass")
                }
            }   .padding()
        }
    
        func performSearch() {
    
        }
    }
    

    and then place the search logic inside performSearch().

提交回复
热议问题