gpt4 book ai didi

ios - 多个 UIPickerViews - UIPickerView 不可见

转载 作者:行者123 更新时间:2023-11-30 13:55:36 29 4
gpt4 key购买 nike

我正在编写一个汽车租赁应用程序,用户必须指定取车和还车位置。

第一步,用户必须选择取货/返回办公室的区域/区域。选择区域后,他必须从该区域可用的办事处列表中进行选择。

为了选择办公室,我使用了 4 个 UIPicker View 。我已经成功处理了该区域的 UIPickers。他们正确地出现了。根据用户选择的区域,我想显示第二个 UIPicker 并用相应的数据填充它。

我在使用第二个 UIPickers(办公室选择器)时遇到问题。我不知道如何使它们可见(另请参阅我的模拟器的屏幕截图)。不知怎的,它们没有出现。下面是我的 Controller 的代码片段。

class NewReservationViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var pickupLocationTextView: UITextField!
@IBOutlet weak var returnLocationTextView: UITextField!

@IBOutlet var pickupZoneUIPicker: UIPickerView!
@IBOutlet var pickupOfficeUIPicker: UIPickerView!
@IBOutlet var returnZoneUIPicker: UIPickerView!
@IBOutlet var returnOfficeUIPicker: UIPickerView!


var zones: [String] = ["Zone1", "Zone2", "Zone3"]
var offices : [String : [String]] = [
"Zone1" : ["Hotel1", "Hotel2", "Hotel3"],
"Zone2" : ["Hotel4", "Hotel5"],
"Zone3" : ["Hotel6", "Hotel7", "Hotel8", "Hotel9"]
]

var pickupZoneUIPickerValue = ""
var pickupOfficeUIPickerValue = ""

var returnZoneUIPickerValue = ""
var returnOfficeUIPickerValue = ""


override func viewDidLoad() {
super.viewDidLoad()

//Hide all available pickers first
pickupZoneUIPicker.hidden = true
pickupOfficeUIPicker.hidden = true
returnZoneUIPicker.hidden = true
returnOfficeUIPicker.hidden = true

//Now init the 2 zone pickers that will be shown first
pickupZoneUIPicker = UIPickerView()
returnZoneUIPicker = UIPickerView()

//Set data source to this controller
pickupZoneUIPicker.dataSource = self
returnZoneUIPicker.dataSource = self

//Set delegates to this class
pickupZoneUIPicker.delegate = self
returnZoneUIPicker.delegate = self

//set the text fields as input views
pickupLocationTextView.inputView = pickupZoneUIPicker
returnLocationTextView.inputView = returnZoneUIPicker

//get Toolbars for both pickers
pickupLocationTextView.inputAccessoryView = getPickerToolbar("pickupZoneDone")
returnLocationTextView.inputAccessoryView = getPickerToolbar("returnZoneDone")

//set default values for zone pickers in case the user clicks Done immediately
pickupZoneUIPickerValue = zones[0]
returnZoneUIPickerValue = zones[0]
}

/*
UIToolbar Callbacks for UIPickers
*/
//called when user has selected the pickup zone in the first text field
func pickupZoneDone(){

//hide the picker
pickupZoneUIPicker.hidden = true

//Now setup a new UIPickerView
pickupOfficeUIPicker = UIPickerView()
pickupOfficeUIPicker.dataSource = self
pickupOfficeUIPicker.delegate = self

//set default value
pickupOfficeUIPickerValue = offices[pickupZoneUIPickerValue]![0]

pickupOfficeUIPicker.hidden = false
pickupOfficeUIPicker.becomeFirstResponder()
pickupOfficeUIPicker.reloadInputViews()
pickupOfficeUIPicker.reloadAllComponents()


}
//called when user has selected the return zone in the second text field
func returnZoneDone(){

}



//Setup the pickerViewToolbar variable of this class
func getPickerToolbar(doneCallbackMethodName: String) -> UIToolbar{


let pickerViewToolbar = UIToolbar()
pickerViewToolbar.barStyle = UIBarStyle.Default
pickerViewToolbar.translucent = true
pickerViewToolbar.sizeToFit()



let doneWithZoneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector(doneCallbackMethodName))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)


pickerViewToolbar.setItems([spaceButton, doneWithZoneButton], animated: false)
pickerViewToolbar.userInteractionEnabled = true

return pickerViewToolbar

}

// MARK: UIPickerView and UIPickerViewDataSource delegate Methods
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
//all pickers only have one component (wheels)
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

if pickerView == pickupZoneUIPicker || pickerView == returnZoneUIPicker {
return zones.count
}
else if pickerView == pickupOfficeUIPicker {
return offices[pickupOfficeUIPickerValue]!.count
}
else //pickerView == returnOfficeUIPicker
{
return offices[returnOfficeUIPickerValue]!.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == pickupZoneUIPicker || pickerView == returnZoneUIPicker{
return zones[row]
}
else if pickerView == pickupOfficeUIPicker {
return offices[pickupOfficeUIPickerValue]![row]
}
else { //if pickerView == returnOfficeUIPicker
return offices[returnOfficeUIPickerValue]![row]
}


}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == pickupZoneUIPicker {
pickupZoneUIPickerValue = zones[row]
}
else if pickerView == returnZoneUIPicker {
returnZoneUIPickerValue = zones[row]
}
else if pickerView == pickupOfficeUIPicker {
pickupOfficeUIPickerValue = offices[pickupZoneUIPickerValue]![row]
}
else { //if pickerView == returnOfficeUIPicker
returnOfficeUIPickerValue = offices[returnOfficeUIPickerValue]![row]
}

}

Zone/region selection Dynamic hotel UIPickerView not showing up

感谢您的帮助。

最佳答案

检查主 Storyboard 中的所有文本字段中是否设置了委托(delegate)

关于ios - 多个 UIPickerViews - UIPickerView 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33671706/

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