Search code examples
jsonswiftnavigationdelegates

Unable to add delegate values to JSON parameters while navigating to popViewController in swift


I have two Viewcontrollers called SearchVC and filterVC for both viewcontrollers i have one JSON API.... here if parameters are nil then i need to show total response..like this i am able to do..

if i get value for parameter from filterVC to SearchVC with popviewcontroller delegate then i need to show that value in parameter.. which is i am unable to do

here i have created and added values to delegate in filterVC:

 struct DataEnteredModelSave {
  var categooryName: String
  var location: String
  var budgetType: String

 }

protocol DataEnteredDelegate: class {
func userDidEnterInformation(info: DataEnteredModelSave)
}
class FilterVC: UIViewController {

weak var delegate: DataEnteredDelegate? = nil

@IBAction func applyBtn(_ sender: Any) {
    if searchServcTf.text != nil{
        let enteredData = DataEnteredModelSave(categooryName: searchServcTf.text ?? "", location: "", budgetType: "")
        delegate?.userDidEnterInformation(info: enteredData)
    }
    else if locationTf.text != nil{
        let enteredData = DataEnteredModelSave(categooryName: "", location: locationTf.text ?? "", budgetType: "")
        delegate?.userDidEnterInformation(info: enteredData)
    }
    else if searchServcTf.text != nil && locationTf.text != nil{
        let enteredData = DataEnteredModelSave(categooryName: searchServcTf.text ?? "", location: locationTf.text ?? "", budgetType: "")
        delegate?.userDidEnterInformation(info: enteredData)
    }
    else{
        let enteredData = DataEnteredModelSave(categooryName: "", location: locationTf.text ?? "", budgetType: "")
        delegate?.userDidEnterInformation(info: enteredData)
    }
    self.navigationController?.popViewController(animated: true)
}

in SearchVC i am adding delegate values to parameters like below: here postServiceCalll not calling while i do popViewcontroller from FilterVc

 class SearchVC: UIViewController, DataEnteredDelegate {

var serviceNmae: String? = ""
var location: String? = ""
var budgetType: String? = ""

@IBAction func filterBtn(_ sender: Any) {
    let vc = Helper.getVcObject(vcName: .SearchFilterVC, StoryBoardName: .Main) as! SearchFilterVC
    vc.delegate = self
    self.checkAndPushPop(vc, navigationController: self.navigationController)
}

func userDidEnterInformation(info: DataEnteredModelSave) {
    print("filter viewcontroller data \(info)")
    self.serviceNmae = info.categooryName
    self.location = info.location
    self.budgetType = info.budgetType
}
func postServiceCalll(){
    
          
    print("serviceName \(serviceNmae) and budgetType \(budgetType)")
    
    let param = ["location" : location, "lat": "", "lng" : "", "nearby": "", "gender": "", "budget": budgetType, "from_date": "", "to_date": "", "min_rate": "", "max_rate": "", "category_select": serviceNmae]

            
    APIReqeustManager.sharedInstance.serviceCall(param: param, method: .post, loaderNeed: true, loadingButton: nil, needViewHideShowAfterLoading: nil, vc: self, url: CommonUrl.search_service_request, isTokenNeeded: true, isErrorAlertNeeded: true, isSuccessAlertNeeded: false, actionErrorOrSuccess: nil, fromLoginPageCallBack: nil){ [weak self] (resp) in
    self?.searchServReq = SearchServiceReqBaseModel(dictionary: resp.dict as NSDictionary? ?? NSDictionary())
        DispatchQueue.main.async {
            self?.tableView.reloadData()
        }
       
    }
}
 }

now here i am getting value in filter viewcontroller data DataEnteredModelSave(categooryName: "Adventure", location: "", budgetType: "")

but in postServiceCalll is not calling and print("serviceName \(serviceNmae) and budgetType \(budgetType)") values not coming.. why where am i mistake.. please do help


Solution

  • Call it

    func userDidEnterInformation(info: DataEnteredModelSave) {
       print("filter viewcontroller data \(info)")
       self.serviceNmae = info.categooryName
       self.location = info.location
       self.budgetType = info.budgetType
       postServiceCalll() //////    << here 
    }