gpt4 book ai didi

ios - swift 同一 UIView 中的多个自定义 UIPickerView

转载 作者:可可西里 更新时间:2023-11-01 02:05:25 25 4
gpt4 key购买 nike

我试图在同一个 viewController 上有两个不同的 pickerView,并让它们代表不同的数据。我尝试了不同的方法来做到这一点,但两个选择器 View 都显示相同的数据,但无法将它们放在一起。任何帮助都会很棒!

这是我的代码:

 @IBOutlet weak var Eventtype: UITextField!


let pickerView = UIPickerView()

var Eventstype = ["Birthday" , "Marriege" , "Get together", "Conference"]

@IBOutlet weak var datepickertxt: UITextField!

var datepicker = UIDatePicker()

@IBOutlet weak var Duration: UITextField!
let duration = UIPickerView()

var Durations = ["6-8","1-5","7-9","10-12"]


override func viewDidLoad() {
super.viewDidLoad()

createDatePicker()
pickerView.delegate = self
pickerView.dataSource = self
duration.delegate = self
duration.dataSource = self
Eventtype.inputView = pickerView
Duration.inputView = duration
// Do any additional setup after loading the view.
}
////For Event Picker view

func numberOfComponents(in pickerView: UIPickerView) -> Int {

return 1


}

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

if (pickerView.tag == 0) {
//pickerView1
return Eventstype.count
} else if (duration.tag == 1){
//pickerView2
return Durations.count
}
return 1
//return DurationTxt.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 0) {

return Eventstype[row]
} else if (duration.tag == 1){

return Durations[row]
}

return ""
}
//return DurationTxt[row]


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

if (pickerView.tag == 0) {
Eventtype.text = Eventstype[row]
self.view.endEditing(false)
}else if (duration.tag == 1){
Duration.text = Durations[row]
self.view.endEditing(true)

}

}

最佳答案

 @IBOutlet weak var Eventtype: UITextField!


let pickerView = UIPickerView()

var Eventstype = ["Birthday" , "Marriege" , "Get together", "Conference"]

@IBOutlet weak var datepickertxt: UITextField!

var datepicker = UIDatePicker()

@IBOutlet weak var Duration: UITextField!
let duration = UIPickerView()

var Durations = ["6-8","1-5","7-9","10-12"]


override func viewDidLoad() {
super.viewDidLoad()

createDatePicker()
pickerView.delegate = self
pickerView.dataSource = self
duration.delegate = self
duration.dataSource = self
Eventtype.inputView = pickerView
Duration.inputView = duration
pickerView.tag = 0
duration.tag = 1
// Do any additional setup after loading the view.
}
////For Event Picker view

func numberOfComponents(in pickerView: UIPickerView) -> Int {

return 1


}

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

if (pickerView.tag == 0) {
//pickerView1
return Eventstype.count
} else if (duration.tag == 1){
//pickerView2
return Durations.count
}
return 1
//return DurationTxt.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 0) {

return Eventstype[row]
} else if (duration.tag == 1){

return Durations[row]
}

return ""
}
//return DurationTxt[row]


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

if (pickerView.tag == 0) {
Eventtype.text = Eventstype[row]
self.view.endEditing(false)
}else if (duration.tag == 1){
Duration.text = Durations[row]
self.view.endEditing(true)

}

}

这是通过标签完成的,否则你可以 checkin 委托(delegate)

if pickerView == duration

关于ios - swift 同一 UIView 中的多个自定义 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43178646/

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