我正在写一个汽车租赁应用程序,用户必须指定汽车的取件和返回位置。
在第一步中,用户必须选择取件/返回办公室的区域/区域。在选择了一个地区之后,他必须从该地区的办事处列表中进行选择。
为了选择办公室,我使用4个UIPicker视图。 我成功处理了UIPickers地区。它们正确显示出来。根据用户选择的区域,我想显示第二个UIPicker并用相应的数据填充它。
我遇到第二个UIPickers(办公室拣货员)的问题。我不知道如何让它们可见(参见我的模拟器的截图)。它们没有显示出来。下面是我的控制器的代码片段。
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]
}
}
感谢您的帮助。
答案 0 :(得分:0)
检查是否在主故事板的所有文本字段中设置了委托