如何使用 UIPickerView 设置第二个组件

Posted

技术标签:

【中文标题】如何使用 UIPickerView 设置第二个组件【英文标题】:How do I setup a second component with a UIPickerView 【发布时间】:2015-04-14 01:43:08 【问题描述】:

我发现了一些与此类似的问题,但没有一个解决方案符合我的情况,而且大多数都在我不知道的 Objective-c 中。

我正在尝试创建一个带有分钟和秒的计时器,但我不知道如何连接我的第二个组件。

这是我目前所拥有的:

TimeViewController.swift

class TimeViewController: UIViewController, UIPickerViewDelegate 

    @IBOutlet weak var timeSegueLabel: UILabel!

    let minutes = Array(0...9)
    let seconds = Array(0...59)

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int 

        return 2
    

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

        return minutes.count

    

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! 

        return String(minutes[row])

    

    override func viewDidLoad() 
        super.viewDidLoad()

    

【问题讨论】:

请注意 UIPickerViewDataSource 方法有一个 component 参数。您需要根据部分返回不同数量的行或行的不同标题,因此在这些方法中只需使用 if 语句或开关来检查部分。 成功了,谢谢 Bluehound 【参考方案1】:

感谢@Bluehound 为我指明了正确的方向。我不得不在 GitHub 上查看一些 objective-c 以了解确切的操作方法,这就是我一直在寻找的答案:

时间视图控制器:

class TimeViewController: UIViewController, UIPickerViewDelegate 

    @IBOutlet weak var timeSegueLabel: UILabel!

    let minutes = Array(0...9)
    let seconds = Array(0...59)

    var recievedString: String = ""

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int 

        return 2

    

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        //row = [repeatPickerView selectedRowInComponent:0];
        var row = pickerView.selectedRowInComponent(0)
        println("this is the pickerView\(row)")

        if component == 0 
            return minutes.count
        

        else 
            return seconds.count
        

    

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! 

        if component == 0 
        return String(minutes[row])
         else 

        return String(seconds[row])
        
    

    override func viewDidLoad() 
        super.viewDidLoad()

        timeSegueLabel.text = recievedString

    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()

    

这是结果:

【讨论】:

【参考方案2】:

可以在两个标签中显示 2 个组件的 Picker

swift 4 xcode 9

class ViewController: UIViewController, UIPickerViewDelegate,  UIPickerViewDataSource 

    @IBOutlet weak var lbl1: UILabel!
    @IBOutlet weak var lbl2: UILabel!
    @IBOutlet weak var picker1: UIPickerView!

    var country = ["UK","US","JAPAN","UAE","CHINA","CANADA","EUROPE","IRAN","INDIA","ITALY" ]
    var capital = [ "LONDON","WASHIGTON DC","TOKYO","ABU DHABI","BEIJING","OTTAWA","BRUSSELS","TEHRAN","NEW DELHI","ROME" ]
    var val = 0

    override func viewDidLoad() 
        super.viewDidLoad()
    

    func numberOfComponents(in pickerView: UIPickerView) -> Int 
        return 2
    

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

        if component == 0 
            return country.count
         else 
            return capital.count
        
    

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

        if component == 0 
            return String(country[row])
         else 
            return String(capital[row])
        
    

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

        if component == 0 
            lbl1.text = country[row]
         else 
            lbl2.text = capital[row]
        
    

【讨论】:

以上是关于如何使用 UIPickerView 设置第二个组件的主要内容,如果未能解决你的问题,请参考以下文章

如何在 UIPickerView 的第一个组件中显示数据,直到第二个组件中特定行的开始?

无法根据 UIPickerview 中第一个组件中的值重新加载第二个组件

根据第一个组件选择在 UIPickerView 第二个组件中显示数据

UIPickerView 选择的第一个组件决定第二个组件的内容不同步

Swift UIPickerView 第一个组件更改第二个组件数据

UIPickerView selectedRowInComponent:当我尝试在第二个组件中设置数据时,0总是先返回零