根据UIPickerView在If语句中的选定行更改标签

时间:2018-05-02 05:30:56

标签: ios swift uipickerview

我仔细搜索过多个页面和帖子,但无法完全得到我需要的内容。我目前正在开发一个项目,其中一个控制器有几个文本字段,输出使用单个UIPickerView,有时标签需要根据用户的选择进行更改。

如何根据UIPickerView选定的行更改标签,尤其是在if语句中?

我制作了一个简单的测试代码和示例来演示。

代码

class TestViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var categoryOutput: UITextField!
@IBOutlet weak var shapeOutput: UITextField!
@IBOutlet weak var lengthDiaOutput: UITextField!
@IBOutlet weak var heightOutput: UITextField!

@IBOutlet weak var dimensionLabel: UILabel!

let category = ["Perimeter", "Area"]
let shape = ["Rectangle", "Circle", "Triangle"]


// MARK: - viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()

    createCategoryPicker()
    createShapePicker()
}


// MARK: - UIPickerViews

// Create a UIPickerView as an input text field for 'Category'.

func createCategoryPicker() {

    let categoryPicker = UIPickerView()
    categoryPicker.delegate = self
    categoryPicker.tag = 1

    categoryOutput.inputView = categoryPicker

    categoryPicker.backgroundColor  = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1.0)
}

// Create a UIPickerView as an input text field for 'Shape'.

func createShapePicker() {
    let shapePicker = UIPickerView()
    shapePicker.delegate = self
    shapePicker.tag = 2

    shapeOutput.inputView = shapePicker

    shapePicker.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1.0) 
    }

}


// MARK: - Extensions

// Create a UIPickerView.

extension TestViewController: UIPickerViewDelegate, UIPickerViewDataSource {

// Set number of lists.

func numberOfComponents(in pickerView: UIPickerView) -> Int {

    return 1
}

// Set number of rows in list.

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    if pickerView.tag == 1 {
        return category.count }
    if pickerView.tag == 2 {
        return shape.count }
    return 0
}

// Set content of list.

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    if pickerView.tag == 1 {
        return category[row] }
    if pickerView.tag == 2 {
        return shape[row] }
    return nil
}

// Grab selection and change label.

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if pickerView.tag == 1 {
        categoryOutput.text = category[row] }
    if pickerView.tag == 2 {
        shapeOutput.text = shape[row]

        if component == 1 {
            dimensionLabel.text = "Diameter" }
        else {
            dimensionLabel.text = "Length" }
     }
}

// Set appearance of contents.

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

    var label: UILabel

    if let view = view as? UILabel {
        label = view } 
    else { label = UILabel() }

    if pickerView.tag == 1 {
        label.text = category[row] }
    if pickerView.tag == 2 {
        label.text = shape[row] }

    label.font = UIFont(name: "Avenir", size: 17)
    label.textColor = .black
    label.textAlignment = .center

    return label
    }
}

这是需要关注的部分。

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if pickerView.tag == 1 {
        categoryOutput.text = category[row] }
    if pickerView.tag == 2 {
        shapeOutput.text = shape[row]

        if component == 1 {
            dimensionLabel.text = "Diameter" }
        else {
            dimensionLabel.text = "Length" }
     }
}

实施例

enter image description here

时,文字标签 维度 不会更改为 直径 >被选中。

2 个答案:

答案 0 :(得分:1)

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if pickerView.tag == 1 {
        categoryOutput.text = category[row] }
    if pickerView.tag == 2 {
        shapeOutput.text = shape[row]

        if row == 1 //Circle{
            dimensionLabel.text = "Diameter" }
        else {
            dimensionLabel.text = "Length" }
     }
}

答案 1 :(得分:0)

两个选择器视图只有1个组件,即组件0。

“圈子”位于第2行(第1行),因此您的#last remove `NaN`s rows c = df.loc['B'].sub(df.loc['A']).dropna() print (c) 1 -65.0 2 -14.0 3 -1.0 4 17.0 dtype: float64 #subtract with NaNs values - fill_value=0 return non NaNs values d = df.loc['x'].sub(df.loc['A'], fill_value=0) print (d) 1 -50.0 2 -21.0 3 -30.0 4 -44.0 5 62.0 6 11.0 7 44.0 dtype: float64 语句需要检查if,而不是row == 1