i made a picker view for country and city selection and it works fine but every time i come to that view, it gives an alert saying "country cannot be left empty" and after dismissing that it works fine. Im unable to get rid of that alert and want to show either "select country" or set a default country.here's my code:
var countryData: JSON = []var cityData: JSON = []var pickerView: UIPickerView!var country = ""var city = ""override func viewDidLoad() { super.viewDidLoad() createPickerView() createCityPickerView() countriesApi() self.tfCity.text = NetworkManeger.getUserDefault(key: "cityName") self.tfCountry.text = NetworkManeger.getUserDefault(key: "countryName") country = NetworkManeger.getUserDefault(key: "countryId") city = NetworkManeger.getUserDefault(key: "cityId")}
above are the functions im calling when view is loaded.Here are how i made the function to get countries:
//COUNTRIES API func countriesApi(){ if NetworkManeger.isConnectedToNetwork(){ NetworkManeger.getRequest(remainingUrl: ApiEndPoint.countries.description, isLoaderShow: true) { [self] (response,data) in debugPrint("response - ",response) debugPrint("data - ",data) let status = response[BaseUrl.shared.apiProjectName]["resCode"].stringValue self.countryData = [] if status == "1"{ self.countryData = response[BaseUrl.shared.apiProjectName]["countries"] self.pickerView.selectRow(0, inComponent: 0, animated: true) let param:[String:Any] = [BaseUrl.shared.apiProjectName :["countryId": country ]] self.getCityApi(params: param) } else{ let status = response[BaseUrl.shared.apiProjectName]["resMsg"].stringValue debugPrint("status - "+status) self.showAlert(Message: response[BaseUrl.shared.apiProjectName]["resMsg"].stringValue) } } }else{ self.showAlert(Message: "networkError".localized) } }
After that i call getCityApi() using:
func getCityApi(params:[String:Any]){ if NetworkManeger.isConnectedToNetwork(){ NetworkManeger.postRequest(remainingUrl: ApiEndPoint.cities.description, parameters: params,isLoaderShow: true, sendHeader: false) { (response,data) in debugPrint("response - ",response) debugPrint("data - ",data) let status = response[BaseUrl.shared.apiProjectName]["resCode"].stringValue self.cityData = [] if status == "1"{ self.cityData = response[BaseUrl.shared.apiProjectName]["cities"] self.pickerView.selectRow(0, inComponent: 0, animated: true) } else{ let status = response[BaseUrl.shared.apiProjectName]["resMsg"].stringValue debugPrint("status - "+status) self.showAlert(Message: response[BaseUrl.shared.apiProjectName]["resMsg"].stringValue) } } }else{ self.showAlert(Message: "networkError".localized) } }
Functions for pickerView:
func createPickerView() { pickerView = UIPickerView() pickerView.delegate = self pickerView.tag = 1 tfCountry.inputView = pickerView } func createCityPickerView() { pickerView = UIPickerView() pickerView.delegate = self pickerView.tag = 2 tfCity.inputView = pickerView } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 // number of session } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { if pickerView.tag == 1 { debugPrint("countryData count - ",countryData.count) return countryData.count // number of dropdown items } else { debugPrint("cityData count - ",cityData.count) return cityData.count // number of dropdown items } } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { if pickerView.tag == 1 { debugPrint("countryData count - ",countryData[row]["name"].stringValue) return countryData[row]["name"].stringValue } else { debugPrint("cityData count - ",cityData[row]["name"].stringValue) return cityData[row]["name"].stringValue } } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { if pickerView.tag == 1 { debugPrint("countryData selection - ",countryData[row]["name"].stringValue) country = countryData[row]["id"].stringValue // selected item tfCountry.text = countryData[row]["name"].stringValue let param:[String:Any] = [BaseUrl.shared.apiProjectName :["countryId": country ]] self.getCityApi(params: param) } else { debugPrint("cityData selection - ",cityData[row]["name"].stringValue) city = cityData[row]["id"].stringValue // selected item tfCity.text = cityData[row]["name"].stringValue } } @objc func donePicker(){ self.view.endEditing(true) }
im calling getCityApi() in both countriesApI() as well as pickerView so if i leave view after selecting country only and come back the cities are displayed automatically as country was selected already. Everything works as i expected but i still keep getting "country cannot be left empty" every time i relaunch the app.