Tôi đang cố gắng tạo hộp văn bản khi được chọn UIPickerView mở ra với các lựa chọn để chọn. Sau khi được chọn, ẩn UIPickerView và mục đã chọn được hiển thị trong hộp văn bản. Tôi đã thử các đoạn mã khác nhau mà tôi tìm thấy trực tuyến nhưng tôi không thể làm cho nó hoạt động được. Nếu ai đó có thể đề xuất một mã hoàn chỉnh cho điều này hoặc cho tôi biết những gì tôi đang làm sai trong mã của tôi, điều đó sẽ cực kỳ tuyệt vời. Cám ơn rất nhiều.Hiển thị trường văn bản UIPickerView được chọn, sau đó ẩn sau khi đã chọn
Đây là mã của tôi:
@IBOutlet var textfieldBizCat: UITextField!
@IBOutlet var pickerBizCat: UIPickerView! = UIPickerView()
var bizCat = ["Cat One", "Cat Two", "Cat Three"]
override func viewDidLoad() {
super.viewDidLoad()
var bizCatCount = bizCat.count
self.textfieldBizCat.inputView = pickerView
}
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return bizCat.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return bizCat[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
textfieldBizCat.text = "\(bizCat[row])"
}
Cảm ơn sự giúp đỡ của bạn Ckouta. Nó bây giờ hiển thị "Cat One" trong trường văn bản, nhưng khi tôi nhấp vào nó, UIPicker sẽ không xuất hiện. – TimberWebDesign
Bạn đã thiết lập đại biểu UITextField từ xib của bạn? Bạn có thể làm điều đó cũng theo chương trình, chỉ cần thêm 'textfieldBizCat.delegate = self'in' viewDidLoad' – tbaranes
Oh, ok quên làm điều đó. Cũng cần phải thêm pickerBizCat.delegate = self to viewDidLoad. Bây giờ nó hoạt động! Cám ơn rất nhiều. – TimberWebDesign