Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

uitextfield - Send emptyText to Service SwiftUI

I want to send empty text inside the searchText to AuthorService() if button value .isCancel is true. So that my author search result will be cleaned.

import SwiftUI
import Combine

struct AuthorSearchBar: View {
        @StateObject var service = AuthorService()
        @Binding var isCancel: Bool
        @State var emptyText = ""
        var body: some View {
            VStack(spacing: 0) {
               HStack {
                Image(systemName: "magnifyingglass")
                if isCancel == false{
                TextField("Search", text: $service.searchText)
                } else if isCancel {
                    TextField("Search", text: $service.searchText = "") 
            //Cannot assign to property: '$service' is immutable
            //Cannot assign value of type 'String' to type 'Binding<String>'
            //Cannot convert value of type '()' to expected argument type 'Binding<String>'
                }
                .padding()
                .onReceive(Just( self.isCancel ? emptyText : service.searchText), perform: { _ in
                    service.fetchData()
                })
            }
        }
}

My service fetchData() gets searchText parameter as below.

so I want to send "" to fetchData if .isCancel is true.

class AuthorService: ObservableObject {
@Published var searchText : String = ""
func fetchData() {
    let parameters = ["index": "authors", "query": "(searchText)" ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ok, still not sure I understood the flow, but let's try...

Having some assumptions (mentioned in comments) the approach might be as follows (original code is not testable so answer was prepared just here - might be typos, adapting is on you, but idea should be clear):

struct AuthorSearchBar: View {

    @StateObject var service = AuthorService()
    @Binding var isCancel: Bool

    var body: some View {
        VStack(spacing: 0) {
           HStack {
             Image(systemName: "magnifyingglass")
             TextField("Search", text: $service.searchText)
                .padding()
           }
            .onReceive(Just(service.searchText)) { text in
                if !text.isEmpty {  // assuming search only for existed text
                   service.fetchData(for: text)
                }
            }
            .onReceive(Just(self.isCancel)) { canceled in
                if canceled {
                    // assuming needed to send on explicit cancel
                    service.fetchData(for: "")
                }
            }
        }
    }
}

// pass what should be sent via argument instead of use property
func fetchData(for text: String) {
    let parameters = ["index": "authors", "query": "(text)" ]

    ...
}

Alternate: no changes to fetchData, but in that case it is not possible to separate case with empty string as entered and on canceled (though it is not clear if it does matter).

   HStack {
     Image(systemName: "magnifyingglass")
     TextField("Search", text: $service.searchText)
        .padding()
   }
    .onReceive(Just(service.searchText)) { text in
        service.fetchData()
    }
    .onReceive(Just(self.isCancel)) { canceled in
        if canceled {
            service.searchText = ""  // << this activates above onReceive
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...