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

第一步,用户必须选择接送办公室的区域/区域。选择区域后,他必须从该区域可用的办公室列表中进行选择。

对于办公室的选择,我使用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]
    }

}


ios - 多个UIPickerViews-UIPickerView不可见-LMLPHP
ios - 多个UIPickerViews-UIPickerView不可见-LMLPHP

谢谢你的帮助。

最佳答案

检查是否在主故事板上的所有文本字段中都设置了委托

10-08 06:24