如何将值从 swift 中的可重用 PickerView.xib 传递出去
How to pass values out of reusable PickerView.xib in swift
我创建了一个可重复使用的 UIPickerView
:
PickerView.swift:
class PickerView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var pickerView: UIPickerView!
var labels: [String] = [] {
didSet {
self.pickerView.reloadAllComponents()
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
Bundle.main.loadNibNamed("PickerView", owner: self, options: nil)
self.addSubview(pickerView)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return labels.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return labels[row]
}
}
此代码使我可以通过将 UIView
的 class 设置为 PickerView
:
来初始化 UIPickerView
并且要将数据添加到 UIPickerView
,我只需创建一个 outlet
并通过插座添加数据。所以,我的 ViewController.swift 文件如下所示:
ViewController.swift:
class ViewController: UIViewController {
@IBOutlet weak var pickerView: PickerView!
override func viewDidLoad() {
super.viewDidLoad()
self.pickerView.labels = ["Label1","Label2","Label3"]
}
}
这是我的问题: 如何将 didSelectRow
值从选择器中传递出来以在 ViewController.swift 中使用?
创建自定义协议
protocol MyPickerDelegate: NSObjectProtocol {
func didSelectSomething(some: String)
}
在 PickerView.swift 中创建实例 like
weak var myDelegate: MyPickerDelegate?
然后在 viewDidLoad
self.pickerView.myDelegate = self
然后让你的UIViewController
符合协议
extension ViewController: MyPickerDelegate {
func didSelectSomething(some: String) {
// self.doStuff()
}
}
然后在 PickerView.swift 调用 didSelectRow
并在其中
self.myDelegate?.didSelectSomething(some: "SomeValue")
我创建了一个可重复使用的 UIPickerView
:
PickerView.swift:
class PickerView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var pickerView: UIPickerView!
var labels: [String] = [] {
didSet {
self.pickerView.reloadAllComponents()
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
Bundle.main.loadNibNamed("PickerView", owner: self, options: nil)
self.addSubview(pickerView)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return labels.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return labels[row]
}
}
此代码使我可以通过将 UIView
的 class 设置为 PickerView
:
UIPickerView
并且要将数据添加到 UIPickerView
,我只需创建一个 outlet
并通过插座添加数据。所以,我的 ViewController.swift 文件如下所示:
ViewController.swift:
class ViewController: UIViewController {
@IBOutlet weak var pickerView: PickerView!
override func viewDidLoad() {
super.viewDidLoad()
self.pickerView.labels = ["Label1","Label2","Label3"]
}
}
这是我的问题: 如何将 didSelectRow
值从选择器中传递出来以在 ViewController.swift 中使用?
创建自定义协议
protocol MyPickerDelegate: NSObjectProtocol {
func didSelectSomething(some: String)
}
在 PickerView.swift 中创建实例 like
weak var myDelegate: MyPickerDelegate?
然后在 viewDidLoad
self.pickerView.myDelegate = self
然后让你的UIViewController
符合协议
extension ViewController: MyPickerDelegate {
func didSelectSomething(some: String) {
// self.doStuff()
}
}
然后在 PickerView.swift 调用 didSelectRow
并在其中
self.myDelegate?.didSelectSomething(some: "SomeValue")