高小高
高小高

Reputation: 37

IOS swift UIPickerview issue

How can I add other picker view in my view controller

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    @IBOutlet weak var UnitTypeLabel: UILabel!
    @IBOutlet weak var UnitTypeSelectBTN: UIButton!

    @IBOutlet weak var UnitTypePicker: UIPickerView!




    let UnitDataArray = ["Unit for Weight","Unit for Length","Unit for Area","Unit for Volume","Unit for Quantity"]
    let UnitforLengthData = ["m","km","cm","mm","yd","ft","in"]
    let UnitforWeightData = ["g","kg","m.t","l.t","sh.t","lb"]


    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return UnitDataArray.count
    }
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return UnitDataArray[row]
    }
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        return UnitTypeLabel.text = UnitDataArray[row]
    }



    override func viewDidLoad() {
        super.viewDidLoad()
        //UnitTypePicker.hidden = true
        UnitTypePicker.delegate = self
        UnitTypePicker.dataSource = self
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Upvotes: 1

Views: 783

Answers (2)

sagar.musale
sagar.musale

Reputation: 595

If you want more number of pickerView then you will have to set those number of components in it like this:

func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
    return 2  //number of pickerViews
  }

By changing the number, you can set those number of pickerView in same viewController.

But if you want to change only dataSet for only one pickerView after any action then you can see this How do I reload/refresh the UIPickerView (with new data array) based on button press?

Upvotes: 1

mkz
mkz

Reputation: 2302

Here is how it should be, you have to implement pickerView viewForRow method to achieve this:

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var pickerView: UIPickerView!

    let UnitDataArray = ["Unit for Weight","Unit for Length","Unit for Area","Unit for Volume","Unit for Quantity"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // pickerView delegate n dataSource
        pickerView.dataSource = self
        pickerView.delegate = self
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return UnitDataArray.count
    }

    //MARK: Delegates
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return UnitDataArray[row]
    }

    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
    {
        var pickerLabel = UILabel()
        pickerLabel.textColor = UIColor.blackColor()
        pickerLabel.text = UnitDataArray[row]
        if let pickerViewFont = UIFont(name: "HelveticaNeue-Thin", size: 30) {
            pickerLabel.font = pickerViewFont
        }
        pickerLabel.textAlignment = NSTextAlignment.Center
        return pickerLabel
    }

    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40.0
    }
}

Upvotes: 1

Related Questions