1
我試圖在下拉列表中選擇器視圖在用戶單擊文本框之前不可見。然後,選擇器視圖在用戶從pickerView中進行選擇後消失。目前,當我點擊文本框時,選擇器視圖根本不顯示。在Swift 3中創建一個下拉列表
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var textBox: UITextField!
@IBOutlet weak var pickerView: UIPickerView!
var choices = ["SortBy:", "Date(ascending)", "Date(descending)","Calories Burned(descending)","Duration(descending)"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.view.endEditing(true)
return choices[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return choices.count
}
fun pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.textBox.text = self.choices[row]
self.pickerView.isHidden = true
print("hello")
}
func textFieldDidBeginEditing(TextField: UITextField){
if TextField == self.textBox {
self.pickerView.isHidden = false
TextField.endEditing(true)
}
}
override func viewDidLoad() {
self.pickerView.isHidden = true
super.viewDidLoad()
}
}
不是一個直接的答案,但你可能想看看https://github.com/skywinder/ActionSheetPicker-3.0 - 我只是一個定製他們的採摘員要做你正在做的事情。 – sschale
謝謝我會檢查出來。 – jean