单个视图上有多个选择器视图 [英] Multiple Picker Views on a single view
问题描述
我已遵循以下 SO问题创建多个选择器视图. 它可以工作,但不完全是我想要的.
I have followed the following SO question to create multiple picker views. It works but not exactly how I want it.
我需要什么:
我的视图将有多个按钮.点击时,每个按钮将在屏幕底部显示一个选择器视图.每个按钮的选择器视图将具有不同的选项.参见下图:
My view will have multiple buttons. On tap each button will show a picker view at the bottom of the screen.Each buttons picker view will have different options. See image below:
我所做的事情:
到目前为止,我已经创建了两个按钮,然后在我的情节提要中添加了两个选择器视图.见下文
So far I have created two buttons and then added two picker views to my storyboard. See Below
代码如下:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet var one: UIPickerView!
@IBOutlet var Two: UIPickerView!
var picker1Options = [String]()
var picker2Options = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
picker1Options = ["Option 1","Option 2","Option 3","Option 4","Option 5"]
picker2Options = ["Item 1","Item 2","Item 3","Item 4","Item 5"]
self.one.hidden = true
self.Two.hidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.tag == 1){
return picker1Options.count
}else{
return picker2Options.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 1){
return "\(picker1Options[row])"
}else{
return "\(picker2Options[row])"
}
}
@IBAction func oneShow(sender: AnyObject) {
self.one.hidden = false
}
@IBAction func twoShow(sender: AnyObject) {
self.Two.hidden = false
}
}
以上代码的输出为:
问题:
如上图所示,尽管我可以显示不同的选择器视图,但它们无法正确显示.我希望每个选择器视图都显示在屏幕的底部.
As you can see in the above image that although I can show different picker views they are not properly displayed. I would like each picker view to be displayed at the end bottom of the screen.
还请注意,我正在使用self.picker.hidden = true
隐藏和取消隐藏选择器视图. 这是隐藏和取消隐藏选择器视图的正确方法吗?有没有更好的方法来实现此功能?
Also please note that I am using self.picker.hidden = true
to hide and unhide the picker views. Is this the correct way of hiding and unhiding picker views? Is there a better way of achieving this functionality?
我应该在情节提要板上将选择器视图彼此重叠放置在屏幕底部,以使它们始终在触发self.picker.hidden = false
时显示在底部吗?
On the storyboard should I place picker views on top of each other at the bottom of the screen so they always appear at bottom on triggering self.picker.hidden = false
?
在情节提要板上实现多个选择器视图的正确方法是什么?
Whats the proper way of implementing multiple picker views on storyboard?
任何帮助将不胜感激.
推荐答案
您在使用UiPickerView时犯了一些大概念错误:
You make some big concept mistake in the usage of the UiPickerView:
1.故事板中仅需要一个UIPickerView.
1.You need only one UIPickerView in your storyboard.
2.选择器视图仅需要一个数据源(单击按钮即可更改其中的值).
2.You need only one data source for the picker view (on button click you gonna change the values in it).
我通过您的代码举了一个例子,但是请注意会有一些错误,因为我是在NotePad ++上编写的(我面前没有Mac).
I make an example by your code but be aware there will be some mistakes because i write it on NotePad++ (i don't have Mac in front of me).
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet var picker: UIPickerView!
var pickerOptions1 = [String]()
var pickerOptions2 = [String]()
var pickerOptions3 = [String]()
var dataSource;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
pickerOptions1 = ["Option 1","Option 2","Option 3","Option 4","Option 5"]
pickerOptions2 = ["Item 1","Item 2","Item 3","Item 4","Item 5"]
pickerOptions3 = ["Something 1","Something 2","Something 3","Something 4","Something 5"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataSource.count;
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "\(dataSiurce[row])"
}
@IBAction func showPickerOne(sender: AnyObject) {
dataSource = pickerOptions1;
[self.picker reloadAllComponents];
}
@IBAction func showPickerTwo(sender: AnyObject) {
dataSource = pickerOptions2;
[self.picker reloadAllComponents];
}
@IBAction func showPickerThree(sender: AnyObject) {
dataSource = pickerOptions3;
[self.picker reloadAllComponents];
}
}
我希望这可以帮助您了解ios中选择器的概念.
I hope this will help you understand the concept of pickers in ios.
这篇关于单个视图上有多个选择器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!