如何在一个视图控制器中使用两个 UIPickerViews? [英] How to use two UIPickerViews in one view controller?

查看:23
本文介绍了如何在一个视图控制器中使用两个 UIPickerViews?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个视图控制器中有两个 UIPickerController .我可以让一个工作,但是当我添加第二个时,我的应用程序崩溃了.这是我用于一个选择器视图的代码:

I have two UIPickerControllers in one view controller. I can get one to work, but when I add a second, my app crashes. Here is the code I use for one picker view:

import UIKit

class RegisterJobPosition: UIViewController, UIPickerViewDelegate {

    @IBOutlet weak var positionLabel: UILabel!

    var position = ["Lifeguard", "Instructor", "Supervisor"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

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

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

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {        
        positionLabel.text = position[row]
    }
}

现在,我怎样才能让第二个拣货员工作?假设我的第二个选择器视图称为 location(另一个称为 position).我尝试在 location 的选择器视图方法中复制代码,但它不起作用.

Now, how can I get a second picker to work? Say my second picker view is called location (the other one is called position). I tried duplicating the code within the picker view methods for location but it doesn't work.

推荐答案

根据我在问题中的信息,我会说您需要设置数据源 &委托方法来处理区分哪个选择器实例正在调用它们的能力.

Based on the information I have in the question, I'd say that you need to set up the data source & delegate methods to handle the ability to distinguish between which picker instance is calling them.

使用标签属性 在选择器视图上是一种策略.

Using the tag property on the picker view is one strategy.

方法中应该有一些 if/else 或 switch 语句,它们具有不同的逻辑,具体取决于引用的是位置还是位置选择器.

There should be some if/else or switch statements in the methods that have varying logic depending on whether it's the location or the position picker that's being referenced.

这篇关于如何在一个视图控制器中使用两个 UIPickerViews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆