Skip to content

Commit 9a6ad71

Browse files
committed
add method setCountryByName
1 parent 1908cf5 commit 9a6ad71

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

Example/MRCountryPicker/ViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ViewController: UIViewController, MRCountryPickerDelegate {
1515
countryPicker.showPhoneNumbers = true
1616
countryPicker.setCountry("SI")
1717
countryPicker.setLocale("sl_SI")
18+
countryPicker.setCountryByName("Canada")
1819
}
1920

2021
func countryPhoneCodePicker(_ picker: MRCountryPicker, didSelectCountryWithName name: String, countryCode: String, phoneCode: String, flag: UIImage) {

MRCountryPicker/Classes/SwiftCountryPicker.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,30 @@ open class MRCountryPicker: UIPickerView, UIPickerViewDelegate, UIPickerViewData
5858
// MARK: - Country Methods
5959

6060
open func setCountry(_ code: String) {
61-
var row = 0
6261
for index in 0..<countries.count {
6362
if countries[index].code == code {
64-
row = index
65-
break
63+
return self.setCountryByRow(row: index)
6664
}
6765
}
68-
69-
self.selectRow(row, inComponent: 0, animated: true)
70-
let country = countries[row]
71-
if let countryPickerDelegate = countryPickerDelegate {
72-
countryPickerDelegate.countryPhoneCodePicker(self, didSelectCountryWithName: country.name!, countryCode: country.code!, phoneCode: country.phoneCode!, flag: country.flag!)
73-
}
7466
}
7567

7668
open func setCountryByPhoneCode(_ phoneCode: String) {
77-
var row = 0
7869
for index in 0..<countries.count {
7970
if countries[index].phoneCode == phoneCode {
80-
row = index
81-
break
71+
return self.setCountryByRow(row: index)
8272
}
8373
}
84-
74+
}
75+
76+
open func setCountryByName(_ name: String) {
77+
for index in 0..<countries.count {
78+
if countries[index].name == name {
79+
return self.setCountryByRow(row: index)
80+
}
81+
}
82+
}
83+
84+
func setCountryByRow(row: Int) {
8585
self.selectRow(row, inComponent: 0, animated: true)
8686
let country = countries[row]
8787
if let countryPickerDelegate = countryPickerDelegate {

0 commit comments

Comments
 (0)