我有多个带有UIPicker视图和完成按钮的文本字段但是UIPicker视图不会在模拟器中显示自己,也不会重新加载数据。
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{
@IBOutlet weak var RoomTypeTextField: UITextField!
@IBOutlet weak var WindowTypeTextField: UITextField!
@IBOutlet weak var ShelterTypeTextField: UITextField!
// Room Type, Window Type and Shelter Variables
//Room Type
var RoomType = ["Bedroom", "Bathroom", "Sitting Room", "Hallway", "Dining Room", "Hallway", "Kitchen", "Conservatory"]
var picker : UIPickerView!
//Window Type
var WindowType = ["Single Galzed", "Double galzed"]
//Shelter Type
var ShelterType = ["Sheltered", "Average", "Exposed"]
//Done button for UIPickerView
var toolBar = UIToolbar()
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: nil, action: "doneUIPicker")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.picker = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 100))
NSNotificationCenter.defaultCenter().addObserver(self, selector: ("updatePicker"), name: UITextFieldTextDidBeginEditingNotification, object: nil)
func updatePicker (){
self.picker.reloadAllComponents()
}
picker.backgroundColor = .whiteColor()
picker.showsSelectionIndicator = true
picker.delegate = self
picker.dataSource = self
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 75/255, green: 75/255, blue: 75/255, alpha: 1)
toolBar.sizeToFit()
toolBar.setItems([spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
RoomTypeTextField.inputView = picker
WindowTypeTextField.inputView = picker
ShelterTypeTextField.inputView = picker
RoomTypeTextField.inputAccessoryView = toolBar
WindowTypeTextField.inputAccessoryView = toolBar
ShelterTypeTextField.inputAccessoryView = toolBar
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if RoomTypeTextField.isFirstResponder() {
return RoomType.count
} else if WindowTypeTextField.isFirstResponder() {
return WindowType.count
} else if ShelterTypeTextField.isFirstResponder() {
return ShelterType.count
}
return 1
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if RoomTypeTextField.isFirstResponder() {
return RoomType[row]
} else if WindowTypeTextField.isFirstResponder() {
return WindowType[row]
} else if ShelterTypeTextField.isFirstResponder() {
return ShelterType[row]
}
return ""
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if RoomTypeTextField.isFirstResponder() {
var itemselected = RoomType[row]
RoomTypeTextField.text = itemselected
} else if WindowTypeTextField.isFirstResponder() {
var itemselected = WindowType[row]
WindowTypeTextField.text = itemselected
} else if ShelterTypeTextField.isFirstResponder() {
var itemselected = ShelterType[row]
ShelterTypeTextField.text = itemselected
}
func doneUIPicker (){
RoomTypeTextField.resignFirstResponder()
WindowTypeTextField.resignFirstResponder()
ShelterTypeTextField.resignFirstResponder()
}
}
}
答案 0 :(得分:0)
你必须在 - (Void)textfieldDidBeginEditing中编写一个代码来重新加载AllComponents。 我在目标C工作,所以不能给你样例代码。但这肯定会奏效。
见下目标C中的示例代码。可能会给你一个想法
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
[Picker reloadAllComponents];
}