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
602 views
in Technique[技术] by (71.8m points)

delegates - 自定义委托过滤器VC Swift5(Custom Delegate Filter VC Swift5)

I'm adding a custom delegate to my app and, for some reason, it is not working.

(我正在向我的应用添加自定义委托,由于某种原因,它无法正常工作。)

My app has a map where I show several markers of different company types.

(我的应用程序有一张地图,其中显示了不同公司类型的几个标记。)

There is also a button that, once pressed, takes me to another viewController where the user can input some filters.

(还有一个按钮,一旦按下该按钮,会将我带到另一个viewController,用户可以在其中输入一些过滤器。)

The user then presses "Apply" which would pass the filtering data to the map viewController.

(然后,用户按下“应用”,这会将过滤数据传递给地图viewController。)

The issue here is that no data is being passed.

(这里的问题是没有数据被传递。)

As reference I followed the guideline https://medium.com/@jamesrochabrun/implementing-delegates-in-swift-step-by-step-d3211cbac3ef which works perfectly fine.

(作为参考,我遵循了指南https://medium.com/@jamesrochabrun/implementing-delegates-in-swift-step-by-step-d3211cbac3ef ,该指南工作得很好。)

Here is the full project code https://github.com/afernandes0001/Custom-Delegate

(这是完整的项目代码https://github.com/afernandes0001/Custom-Delegate)

I use Firebase but code below just shows pieces related to the delegate.

(我使用Firebase,但下面的代码仅显示与委托相关的部分。)

mapViewController - you will notice that I added a print to the prepareForSegue.

(mapViewController-您会注意到我在prepareForSegue中添加了打印。)

When first loading the app and clicking "Search" button it shows nav1 as nil (which is expected) but, if I click Search and Apply (in filterVC), that print is never done.

(首次加载应用程序并单击“搜索”按钮时,它将nav1显示为nil(这是预期的),但是,如果我单击“搜索并应用”(在filterVC中),则该打印永远不会完成。)

import UIKit
import MapKit

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, FilterVCDelegate {

    @IBOutlet weak var map: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        map.register(MyAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "clinicDetailsSegue" {
            let clinicsDetailsViewController = segue.destination as! ClinicsDetailsViewController
            clinicsDetailsViewController.id = self.note.mapId
        } else if segue.identifier == "searchSegue" {
            print("segue call")
            let nav1 = segue.destination as? UINavigationController
            print("nav1 (nav1)")
            if let nav = segue.destination as? UINavigationController, let filterVC = nav.topViewController as? FilterViewController {
                filterVC.delegate = self
            }
      }
    }

    func chosenData(clinicNameFilter: String, stateFilter: String, cityFilter: String, esp1Filter: String, esp2Filter: String) {
        print("Received data (clinicNameFilter), (stateFilter), (cityFilter), (esp1Filter), (esp2Filter)")
    }
}

FilterViewController

(FilterViewController)

import UIKit

protocol FilterVCDelegate: class {
    func chosenData(clinicNameFilter: String, stateFilter: String, cityFilter: String, esp1Filter: String, esp2Filter: String)
}


class FilterViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    weak var delegate: FilterVCDelegate?

    var selectedName = ""

    var statesJSON = [Estado]()
    var cities = [Cidade]()
    var state : Estate? // Selected State identifier
    var city : City? // Selected City identifier
    var selectedState = "" // Used to retrieve info from Firebase
    var selectedCity = "" // Used to retrieve info from Firebase

    var specialtiesJSON = [Specialty]()
    var specialties2 = [Specialty2]()
    var specialty1 : Specialty? // Selected Specialty1 identifier
    var specialty2 : Specialty2? // Selected Specialty2 identifier
    var selectedSpecialty1 = ""
    var selectedSpecialty2 = ""

    @IBOutlet weak var clinicName: UITextField!

    @IBOutlet weak var statePicker: UIPickerView!
    @IBOutlet weak var esp1Picker: UIPickerView!
    @IBOutlet weak var esp2Picker: UIPickerView!

    override func viewDidLoad() {
        readJsonStates()
        readJsonSpecialties()
        super.viewDidLoad()
        clinicName.text = ""
    }


    @IBAction func applyFilter(_ sender: Any) {
        if clinicName.text == nil {
            clinicName.text = ""
        }

        if selectedState != "" {
            if selectedCity != "" {
                if selectedSpecialty1 != ""{
                    if selectedSpecialty2 != "" {

                       delegate?.chosenData(clinicNameFilter: clinicName.text!, stateFilter: selectedState, cityFilter: selectedCity, esp1Filter: selectedSpecialty1, esp2Filter: selectedSpecialty2)
                        let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController]
                        self.navigationController?.popToViewController(viewControllers[viewControllers.count - 2], animated: true)
                    } else {
                        print("Fill in all filter data")
                    }
                } else {
                        print("Fill in all filter data")
                }
            } else {
                         print("Fill in all filter data")
            }
        } else {
                        print("Fill in all filter data")
        }
    }


    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        esp1Picker.reloadComponent(0)
        esp2Picker.reloadComponent(0)
        statePicker.reloadAllComponents()

        if pickerView == statePicker {
            if component == 0 {
                self.state = self.statesJSON[row]
                self.coties = self.statesJSON[row].cities
                statePicker.reloadComponent(1)
                statePicker.selectRow(0, inComponent: 1, animated: true)
            } else {
                self.city = self.cities[row]
                statePicker.reloadAllComponents()
            }
        } else if pickerView == esp1Picker {
            self.specialty1 = self.specialtiesJSON[row]
            self.specialties2 = self.specialtiesJSON[row].specialty2
            esp1Picker.reloadComponent(0)
            esp2Picker.reloadComponent(0)
            esp2Picker.selectRow(0, inComponent: 0, animated: true)

        } else if pickerView == esp2Picker {
            self.specialty2 = self.specialties2[row]
            esp1Picker.reloadComponent(0)
            esp2Picker.reloadComponent(0)
        }

        let indexSelectedState = statePicker.selectedRow(inComponent: 0)
        let indexSelectedCity = statePicker.selectedRow(inComponent: 1)
        let indexSelectedEsp1 = esp1Picker.selectedRow(inComponent: 0)
        let indexSelectedEsp2 = esp2Picker.selectedRow(inComponent: 0)

        if indexSelectedState >= 0 {
            if indexSelectedCity >= 0 {
                selectedState = estadosJSON[indexSelectedState].name
                selectedCity = cidades[indexSelectedCity].name
            }
        }

        if indexSelectedEsp1 >= 0 {
            if indexSelectedEsp2 >= 0 {
                selectedSpecialty1 = specialtiesJSON[indexSelectedEsp1].name
                selectedSpecialty2 = specialtiesJSON[indexSelectedEsp1].specialty2[indexSelectedEsp2].name
            }
        }
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        if pickerView == statePicker {
            return 2
        } else if pickerView == esp1Picker {
            return 1
        } else if pickerView == esp2Picker {
            return 1
        }
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView == statePicker {
            if component == 0 {
                return statesJSON.count
            } else {
                return cities.count
            }
        } else if pickerView == esp1Picker {
            return self.specialtiesJSON.count

        } else if pickerView == esp2Picker {
            return specialties2.count
        }
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        var rowTitle = ""
        let pickerLabel = UILabel()

        pickerLabel.textColor = UIColor.black

        if pickerView == statePicker {
            if component == 0 {
                rowTitle = statesJSON[row].name
            } else {
                rowTitle = cities[row].name
            }
        } else if pickerView == esp1Picker {
            rowTitle = specialtiesJSON[row].name
        } else if pickerView == esp2Picker {
            rowTitle = specialties2[row].name
        }

        pickerLabel.text = rowTitle
        pickerLabel.font = UIFont(name: fontName, size: 16.0)
        pickerLabel.textAlignment = .center

        return pickerLabel
    }

    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        if pickerView == statePicker {
            if component == 0 {
                return 50
            } else {
                return 300
            }
        }
        return 300
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }

    func readJsonStates() {
        let url = Bundle.main.url(forResource: "StatesAndCities", withExtension: "json")!
        do {
            let data = try Data(contentsOf: url)
            let jsonResult = try JSONDecoder().decode(RootState.self, from: data)

            //handles the array of countries on your json file.
            self.statesJSON = jsonResult.state
            self.cities = self.statesJSON.first!.cities

        } catch {

        }
    }

    func readJsonSpecialties() {
        let url = Bundle.main.url(forResource: "Specialties", withExtension: "json")!
        do {
            let data = try Data(contentsOf: url)
            let jsonResult = try JSONDecoder().decode(RootEsp.self, from: data)

            //handles the array of specialties on your json file.
            self.specialtiesJSON = jsonResult.specialty
            self.specialties2 = self.specialtiesJSON.first!.specialty2
        } catch {
        }
    }

}

Any idea why, when I click ApplyFilter, delegate is not updated in the MapViewController?

(任何想法为什么当我单击ApplyFilter时,MapViewController中的委托未更新?)

Thanks

(谢谢)

  ask by André Fernandes translate from so

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

1 Answer

0 votes
by (71.8m points)
等待大神答复

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

...