gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:03:50 26 4
gpt4 key购买 nike

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

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

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

这是我目前所拥有的:

enter image description here

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()

}
}

最佳答案

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

时间 View Controller :

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()

}

这是结果:

enter image description here

关于swift - 如何使用 UIPickerView 设置第二个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29617835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com