将按钮添加到 UIPickerView 以转到下一个数字

Posted

技术标签:

【中文标题】将按钮添加到 UIPickerView 以转到下一个数字【英文标题】:Add button to UIPickerView to go to next number 【发布时间】:2017-09-12 18:15:23 【问题描述】:

我的pickerView 上有 3 列,它代表一个 3 位数字。有什么我可以添加到UIButton 的功能来使***转动吗?例如,当它显示“119”时,按下UIButton,它应该转到“120”。到目前为止,这是我所拥有的:

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate 

@IBOutlet weak var label: UILabel!
@IBOutlet weak var pickerView: UIPickerView!

let numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

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


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


func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    return numbers.count


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

    let val1 = numbers[pickerView.selectedRow(inComponent: 0)]
    let val2 = numbers[pickerView.selectedRow(inComponent: 1)]
    let val3 = numbers[pickerView.selectedRow(inComponent: 2)]

    label.text = "\(val1) \(val2) \(val3)"

【问题讨论】:

【参考方案1】:

这是您的简化代码。 buttonPressed 将导致 pickerView 转到下一个号码。当它到达 999 时,它又回到 000。

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate 
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var pickerView: UIPickerView!

    override func viewDidLoad() 
        super.viewDidLoad()

        changeLabelText()
    

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

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

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

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

    fileprivate func num(_ i: Int) -> Int 
        return pickerView.selectedRow(inComponent: i)
    

    @IBAction func buttonPressed() 
        let currentNum = num(0) * 100 + num(1) * 10 + num(2)
        let nextNum = currentNum + 1

        pickerView.selectRow(nextNum % 1000 / 100, inComponent: 0, animated: true)
        pickerView.selectRow(nextNum % 100 / 10, inComponent: 1, animated: true)
        pickerView.selectRow(nextNum % 10, inComponent: 2, animated: true)

        changeLabelText()
    

    fileprivate func changeLabelText() 
        label.text = "\(num(0)) \(num(1)) \(num(2))"
    

【讨论】:

【参考方案2】:

当然,使用selectRow 方法,它将选择特定列(又名组件)的一行。该方法还可以选择对此更改进行动画处理。向情节提要添加一个按钮并将其连接到如下方法:

@IBAction func go120(_ sender: Any) 
  pickerView.selectRow(1, inComponent: 0, animated: true)
  pickerView.selectRow(2, inComponent: 1, animated: true)
  pickerView.selectRow(0, inComponent: 2, animated: true)  

【讨论】:

以上是关于将按钮添加到 UIPickerView 以转到下一个数字的主要内容,如果未能解决你的问题,请参考以下文章

用js实现两个按钮效果,上一页 下一页,点上一页按钮页面跳转到上一页,点下一页按钮页面跳转到下一页。

按钮跳转到下一个锚点

Laravel 提交按钮不跳转到下一页和上一页

导航抽屉,处理后退按钮以转到以前的片段?

kivy 按钮按下以转到多个屏幕之一

简单的提交表单以转到页面