How to set the first object in the array as default selected in Country Picker

The app has login page, to be able to login user clicks on the animation "Select your country". When user clicks on that, country list as picker appears as pop-up. Here the first country is Afghanistan and the picker is already on it, but when user clicks OK, it does not select it because as you know picker works if you scroll down or up.

Now I want to set the first object (in my case it is Afghanistan) from the array default selected.

Here is the code:

 @IBAction func chooseCountryClicked(_ sender: Any) {
                
        let alert = AlertFactory.alert(title: ExampleLocalizedString.getLocalizedString("choose_country"),
                                       pickerStrategy: CountryPickerStrategy(withInitialCode: selectedCountryCode),
                                       delegate: self)

        self.present(alert, animated: false, completion: nil)
    }

I have a variable for:

var selectedCountryCode: String? 

I hope that I explained myself good enough.

Appreciate your help. Thanks.

Why don't you initialise selectedCountryCode with the default value ?

We miss information:

  • I understand AlertFactory and CountryPickerStrategy are classes of yours. What are they ?
  • Where is the OK action ?

So, please show code.

//Countries
var countries: [Country] = CountryLanguageManager.shared().getAllCountriesToDisplay()

// Function that gets entries for picker view
func getEntries() -> [String] {

        countries =  countries.sorted {

            ExampleLocalizedString.getLocalizedString($0.CountryStringID) <

               ExampleLocalizedString.getLocalizedString($1.CountryStringID) }

        let countryCodes = countries.map{$0.isoCode}

        return countryCodes

    }

// Function that gets initially selected

func getInitiallySelectedEntry() -> String {

        if let safeCode = initialcode {

            return safeCode

        } else {

            return self.countries[0].isoCode

        }

    }

// Setting up OK Button

func setupOkButton() {

        let action = VWConnectUIAlertAction(title: ExampleLocalizedString.getLocalizedString("OK"),

                                            style: UIAlertActionStyle.default,

                                            accesibilityID: AccessibilityIdConstants.onBoarding_country_picker_ok) { (_) in

            self.onOkClicked()

        }

        self.addAction(action)

    }


How to set the first object in the array as default selected in Country Picker
 
 
Q