2015-05-28 83 views
4

我發現了類似的問題,他們的回答很有幫助,但我陷入了最後一件事。當我點擊一個字段時,我試圖讓pickerView出現,然後當數據被選中時,我想讓pickerView隱藏。我能夠從pickerView中獲取數據以隱藏,但是,仍然存在pickerView後面的灰色矩形。如果我在屏幕上點擊,而不是在字段或pickerView上,灰色的矩形會隱藏,然後在我點擊下一個字段時重新出現,只有在選擇新數據後纔會保留,直到我點擊某個空白屏幕。Swift - 隱藏pickerView後選擇的值

我在下面包括我的代碼,你會看到我已經嘗試了幾個選項。這些並不是我所嘗試過的,但經過多次嘗試,我開始只是發表評論而不是刪除,所以我可以記住我所做的。我對編碼非常陌生,所以我非常感謝這個網站作爲資源,並感謝您的協助。

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate { 

    @IBOutlet var enterSeason: UITextField! 

    @IBOutlet var enterSport: UITextField! 

    var dataPickerView = UIPickerView()  
    var season = ["2013", "2014", "2015"] //multi-season 
    var sport = ["Baseball", "Football", "Basketball", "Hockey"] 
    var activeDataArray = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     enterSeason.inputView = dataPickerView 
     enterSport.inputView = dataPickerView 
     dataPickerView.delegate = self 
     dataPickerView.dataSource = self 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func textFieldDidBeginEditing(textField: UITextField) { 
     activeDataArray = [] //clear out the clicked field data array 
     if textField == enterSeason { 
      activeDataArray = season 
     } else 
      if textField == enterSport { 
       activeDataArray = sport 
      } 
     dataPickerView.reloadAllComponents() 
     dataPickerView.hidden = false 
    } 

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
     return 1 
    } 

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
     return activeDataArray.count 
    } 

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { 
     return activeDataArray[row] as! String 
    } 

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
     if activeDataArray == season { 
      enterSeason.text = season[row] as String 
     } 
     else if activeDataArray == sport { 
      enterSport.text = sport[row] as String 
     } 
     //trying to hide the dataPicker 
     dataPickerView.hidden = true 
     //dataPickerView.reloadAllComponents() 
     //self.dataPickerView.resignFirstResponder() 
     //self.dataPickerView.frameForAlignmentRect(CGRectMake(0, 900, 375, 219)) 

    } 

    //function to hide data in 
    func textFieldShouldReturn(textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 
     return true 
    } 

    //funtion to hide keyboard when screen is tapped 
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
     self.view.endEditing(true) 
    } 

} 

回答

13

如果你想隱藏pickerView,試試這個:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    if activeDataArray == season { 
     enterSeason.text = season[row] as String 
    } 
    else if activeDataArray == sport { 
     enterSport.text = sport[row] as String 
    } 
    //trying to hide the dataPicker 
    self.view.endEditing(true) 
    //dataPickerView.reloadAllComponents() 
    //self.dataPickerView.resignFirstResponder() 
    //self.dataPickerView.frameForAlignmentRect(CGRectMake(0, 900, 375, 219)) 

} 
+0

我想你的建議,但有一個錯誤。建議的解決方法是在'endEditing'和open paren'之間插入一個逗號('。當我添加該逗號時,它仍然存在錯誤,並且建議的解決方法是添加另一個逗號,並且on,on。 – Greg

+0

@Greg我很抱歉,我忘了你正在使用swift而不是objective-c – Bannings

1

一個隱藏選擇器視圖的方法是:

self.view.endEditing(true) 

這爲我工作。

+0

This works .. ..在我的情況下..當pickerView是「活動或打開」,我想傳遞給另一個ViewController。謝謝! – xhinoda

0

我想辭職文本字段的第一個響應者在didSelectRow

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
if activeDataArray == season { 
    enterSeason.text = season[row] as String 
    enterSeason.resignFirstResponder() 
} 
else if activeDataArray == sport { 
    enterSport.text = sport[row] as String 
    enterSport.resignFirstResponder() 
} 

}