2015-11-12 52 views
0

我正在寫一個汽車租賃應用程序,用戶必須指定汽車的提貨和返回位置。多個UIPickerViews - UIPickerView不可見

在第一步中,用戶必須選擇取貨/退貨辦事處的區域/區域。在他選擇了一個地區之後,他必須從該地區的可用辦公室列表中進行選擇。

對於我選擇的辦公室,我使用4個UIPicker視圖。 我已經設法處理區域UIPickers。他們顯示正確。根據用戶選擇的區域,我想顯示第二個UIPicker並用相應的數據填充它。

我有第二個UIPICKS(辦公室採摘員)的問題。我不知道如何讓它們可見(請參閱我的模擬器的截圖)。它們不會顯示出來。以下是我的控制器的代碼片段。

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

感謝您的幫助。

+0

你試過呼籲'pickupLocationTextView'在''pickupZoneDone' reloadInputViews()'? – Losiowaty

+0

是的,不幸的是,沒有任何反應。 – slashburn

+0

您是否嘗試過使用.tag屬性?查看更多信息 Spencer

回答

0

檢查委託在主腳本的所有文本框設置