RVCountryPicker 1.2.5

RVCountryPicker 1.2.5

Randika Vishman维护。




countrypicker-ios-swift

用于 iOS iPhone 应用的代表各国的小旗标记的 Country Picker,用 Swift 编写。目前支持 226 个国家。

Real Device iPhone6S Vs iPhone6S Simulator
展示示例应用截图来演示方法

特性

  • 您可以轻松获取包含以下属性的国家对象数组
    • 按字母顺序排列的索引(0 - 225)
    • 国家名称
    • ISO 国家代码(例如,斯里兰卡 - LK)
    • 国家的地区 ID(例如,斯里兰卡 - _LK)
    • 标志图像名称(来自库中包含的 226 个标志图像文件之一)
  • 您只需 5 行代码即可集成上述 CountryPickerTableViewController(以下步骤中说明)

开发路线图

  • DisplayOnlyCountriesArr
  • ShowExceptCountriesArr
  • 搜索栏和启用搜索栏
  • 索引滚动条实现和启用索引滚动条
  • CountryPickerGridViewController & CountryPickerGridView
  • 标志图像优化
  • 自定义 iOS 默认选择器视图和搜索栏
  • UI 主题选项

安装

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它

$ gem install cocoapods

要使用 CocoaPods 将 CountryPicker 集成到您的 Xcode 项目中,请在 Podfile 中指定它,如下所示

将 pod 'countrypicker-ios-swift', '~> 1.2' 添加到您的 Podfile 中,类似于以下内容

target '<Your App Target Name>' do
  pod 'RVCountryPicker', '~> 1.2.4'
end

然后在您的终端中运行 pod install,或者从 CocoaPods.app 中运行。

用法

在您的 AppDelegate.swift 文件中,如下导入 CountryPicker

import RVCountryPicker

然后,在 application(_:didFinishLaunchingWithOptions:) 方法中,如下初始化 CountryListManager 实例。

let countryListManager = CountryListManager.shared
countryListManager.processCountriesList(countryListManager: countryListManager)

然后,可以初始化 CountryPickerTableViewController 并将其推入 UINavigationController 视图控制器堆栈,如下所示

import UIKit
import RVCountryPicker

class ViewController: UIViewController {

    let tableViewController = CountryPickerTableViewController(style: .plain)
    
    override func viewDidLoad() {
	    super.viewDidLoad()
	
	    // CountryPickerTableViewDelegate
	    tableViewController.delegate = self
        
        // TextFieldDelegate
        countryTxtFld.delegate = self
        
        // fetch a list of countries
        let countryListManager = CountryListManager.shared
        let countriesArr = countryListManager.countries
        print("Countries: \(String(describing: countriesArr))")
    }
}

extension ViewController: UITextFieldDelegate {
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
    	 // let's keep the keyboard hidden always for this demo's purpose
        textField.resignFirstResponder()
        
        // push our CountryPickerTableViewController
        self.navigationController?.pushViewController(tableViewController, animated: true)
        
        // Or, you can modally push the CountryPickerTableViewController (as it suites for your application), if so you have to change the presentationMethod to Modal as follows, so it knows how to dismiss it
        tableViewController.presentationMethod = .MODAL
        
        // Then present it
        self.present(tableViewController, animated: true, completion: nil)
    }
}

extension ViewController: CountryPickerTableViewControllerDelegate {
    
    func didSelectCountry(country: Country?) {
        if country != nil {
            self.countryTxtFld.text = country?.name
            self.countryFlagImgVw.image = country?.flagImage
            self.countryCodeLbl.text = "ISO Country Code: \(country!.isoCountryCode)"
        }
    }
}

请记住,您必须实现 CountryPickerTableViewControllerDelegate 协议的 didSelectCountry(country:) 方法,以接收有关所选国家行的相关信息作为国家对象。(请参阅示例中的最后一个扩展)

Q: 为什么你应该使用这个?

A: 因为,你不需要再浪费时间重新发明轮子。尽管你可以这样做,你也很聪明。CountryPicker iOS Swift 库非常轻量级且非常灵活,就像一块你可以随意玩耍和构建到你想要的任何目的的粘土。

注意

- If you found anything wrong which I have done or may be a bug or any improvements suggestion, please help me to improve this codebase.
- You can always add up your valuable utility methods to this class which is related to the purpose of this library.
- Don't forget to check out the Demo project simple implementation to get some more ideas.

许可证

此源代码在 MIT 许可证下提供。

Copyright (c) 2016 Randika Vishman

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
IN THE SOFTWARE.