0

我正在研究拳击计时器。我在屏幕上有 3 个选择器视图。一个用于回合,一个用于每回合的时间,一个用于休息。我已经获得了打印所选选项的按钮,但这只是在用户从每个选项中选择了一些内容之后。因为它在有动作之前不会触发。如果用户从头开始点击,则显示 0 轮、0 次、0 次休息,而不是每个轮次中的第一个数组选择。我将乞求参数设置为每个数组的第一个数字,但我相信有更好的方法来做到这一点。如何在不需要用户交互的情况下调用蝙蝠右侧的选定行!?谢谢!

导入 UIKit

类 ViewController:UIViewController、UIPickerViewDelegate、UIPickerViewDataSource{

@IBOutlet weak var roundsPicker: UIPickerView!
@IBOutlet weak var timePicker: UIPickerView!
@IBOutlet weak var restPicker: UIPickerView!

var roundsPickerOptions = Array(1...15)
var timePickerOptions = [1, 2, 3, 4, 5]
var restPickerOptions = [30, 60, 90, 120]

var rounds = 1
var time = 1
var rest = 30

@IBAction func startButtonPressed(_ sender: Any) {
    print ("\(rounds) Rounds, \(time) Min, \(rest) Seconds of Rest")
}

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

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView.tag == 1 {
            return roundsPickerOptions.count
        } else if pickerView.tag == 2 {
            return timePickerOptions.count
        } else {
            return restPickerOptions.count
        }
    }

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView.tag == 1 {
        return "\(roundsPickerOptions[row]) rounds"
    }else if pickerView.tag == 2 {
        return "\(timePickerOptions[row]) min."
    }else {
        return "\(restPickerOptions[row]) sec."
    }
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
    switch pickerView {
case roundsPicker:
    rounds = roundsPickerOptions[row]
case timePicker:
    time = timePickerOptions[row]
case restPicker:
        rest = restPickerOptions[row]
    
    default:
        break
    }
    
}

}

4

0 回答 0