如何 return 在以编程方式创建的 UIPicker 委托中选择值
How to return selected value in UIPickerDelegate when created programatically
我有一个 viewController
,上面有一堆输入。其中有些是普通文本,但有些需要选择器输入。我决定将每个 PickerDelegate
和 PickerDataSource
的实现移动到一个单独的文件中,以使事情更清晰一些。数据正在出现,但在选择器的 select 上,我不知道如何让事件在我的 viewController 中触发。目前它只在 PickerDelete class.
中触发
一些基本的布局信息是
class MyClass: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let jobTypePickerValues = ["value1", "value2"]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return jobTypePickerValues.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return jobTypePickerValues[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
//println("I want this in the view controller that set up the picker")
}
在显示选择器的视图控制器中,我有以下内容
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myClassSource = MyClass()
pickerView = UIPickerView()
pickerView.dataSource = myClassSource
pickerView.delegate = myClassSource
// set the input view of the job type to be a picker view
textInput.inputView = pickerView
textInput.text = jobTypePickerValues[0]
}
您需要创建一个自定义委托并在您创建 pickerView 的 ViewController
中实施。
protocol CustomPickerDelegate {
func selectedItem(item: String)
}
现在在 MyClass
中创建此协议的实例并在 `ViewController``
中实现
class MyClass: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let jobTypePickerValues = ["value1", "value2"]
var delegate: CustomPickerDelegate?
.. Other code and methods
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
delegate?. selectedItem(jobTypePickerValues[row])
}
}
现在在viewDidLoad
中设置委托并实现协议的方法。
class ViewController: UIViewController, CustomPickerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
myClassSource = MyClass()
myClassSource.delegate = self
pickerView = UIPickerView()
pickerView.dataSource = myClassSource
pickerView.delegate = myClassSource
// set the input view of the job type to be a picker view
textInput.inputView = pickerView
textInput.text = jobTypePickerValues[0]
}
func selectedItem(item: String) {
textInput.text = item
}
}
我有一个 viewController
,上面有一堆输入。其中有些是普通文本,但有些需要选择器输入。我决定将每个 PickerDelegate
和 PickerDataSource
的实现移动到一个单独的文件中,以使事情更清晰一些。数据正在出现,但在选择器的 select 上,我不知道如何让事件在我的 viewController 中触发。目前它只在 PickerDelete class.
一些基本的布局信息是
class MyClass: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let jobTypePickerValues = ["value1", "value2"]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return jobTypePickerValues.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return jobTypePickerValues[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
//println("I want this in the view controller that set up the picker")
}
在显示选择器的视图控制器中,我有以下内容
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myClassSource = MyClass()
pickerView = UIPickerView()
pickerView.dataSource = myClassSource
pickerView.delegate = myClassSource
// set the input view of the job type to be a picker view
textInput.inputView = pickerView
textInput.text = jobTypePickerValues[0]
}
您需要创建一个自定义委托并在您创建 pickerView 的 ViewController
中实施。
protocol CustomPickerDelegate {
func selectedItem(item: String)
}
现在在 MyClass
中创建此协议的实例并在 `ViewController``
class MyClass: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let jobTypePickerValues = ["value1", "value2"]
var delegate: CustomPickerDelegate?
.. Other code and methods
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
delegate?. selectedItem(jobTypePickerValues[row])
}
}
现在在viewDidLoad
中设置委托并实现协议的方法。
class ViewController: UIViewController, CustomPickerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
myClassSource = MyClass()
myClassSource.delegate = self
pickerView = UIPickerView()
pickerView.dataSource = myClassSource
pickerView.delegate = myClassSource
// set the input view of the job type to be a picker view
textInput.inputView = pickerView
textInput.text = jobTypePickerValues[0]
}
func selectedItem(item: String) {
textInput.text = item
}
}