单个视图上的多个选择器视图

时间:2015-11-30 11:42:22

标签: ios swift swift2 uipickerview

我已按照以下SO question创建了多个选择器视图。 它有效但不完全是我想要的。

我需要什么:

我的视图会有多个按钮。点击时,每个按钮都会在屏幕底部显示一个选择器视图。每个按钮选择器视图将有不同的选项。见下图:

enter image description here

我做了什么:

到目前为止,我创建了两个按钮,然后在我的故事板中添加了两个选择器视图。见下文

enter image description here

代码如下:

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
}

}

以上代码的输出为:

enter image description here

问题:

正如您在上面的图片中看到的那样,虽然我可以显示不同的选择器视图,但它们没有正确显示。我希望每个选择器视图都显示在屏幕的最底部。

另请注意,我使用self.picker.hidden = true来隐藏和取消隐藏选择器视图。 这是隐藏和取消隐藏选择器视图的正确方法吗?有没有更好的方法来实现此功能?

在故事板上,我应该在屏幕底部将选择器视图放在彼此的顶部,以便它们始终显示在触发self.picker.hidden = false的底部吗?

在故事板上实现多个选择器视图的正确方法是什么?

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

您可以使用文本字段而不是按钮,并将选取器视图添加为textfield的输入视图。并且基于文本字段,您可以更改数组的内容并在pickerview中显示。

Please find image in that i used age picker and team picker.

修改

-(void) viewWillAppear:(BOOL)animated
{
    [self.PickerVw removeFromSuperview];
    txtAge.inputView = self.PickerVw;
    txtSportName.inputView = self.PickerVw;
}

#pragma mark - UItextfield delegate Methods
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    arr1 = [NSMutableArray new];
    if (textField.tag == 1)
    {
        int min =  9;
        int max = 100;
        for (int i = min ; i < max; i++)
        {
         [arr1 addObject:[NSString stringWithFormat:@"%d",(i+1)]];
        }
        [textField becomeFirstResponder];
        self.picker.tag = 1;
     }
    if (textField.tag == 2)
    {
        [self.picker selectRow:0 inComponent:0 animated:NO];

        arr1 = [arrSports mutableCopy];
        [textField becomeFirstResponder];

        self.picker.tag = 2;

    }
[self.picker reloadAllComponents];
} 

答案 1 :(得分:2)

你在使用UiPickerView时犯了一些大的概念错误:

1.您的故事板中只需要一个UIPickerView。

2.您只需要一个数据源用于选择器视图(在按钮上单击,您将更改其中的值)。

我通过你的代码做了一个例子,但是要注意会有一些错误,因为我在NotePad ++上写了它(我面前没有Mac)。


    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中的拣货员的概念。

答案 2 :(得分:0)

这是关于Apple如何显示/隐藏datePicker的{​​{3}},它应指向正确的方向。