我试图将完成按钮添加到我的选择器视图中,因为当我滑动选择项目时,uipickerview会自动选择滑行,所以我更喜欢按钮完成:
@IBOutlet var tfProjet: UITextField!
var pvProjetData = ["-Choisir-", "Rachat de crédits", "Renégociation de crédits"]
override func viewDidLoad() {
super.viewDidLoad()
let pickerView = UIPickerView()
if DeviceType.IS_IPHONE_4_OR_LESS {
pickerView.frame=CGRectMake(0, 200, view.frame.width, 100)
} else if DeviceType.IS_IPHONE_5 {
pickerView.frame=CGRectMake(0, 200, view.frame.width, 200)
} else if DeviceType.IS_IPHONE_6 {
pickerView.frame=CGRectMake(0, 200, view.frame.width, 200)
} else if DeviceType.IS_IPHONE_6P {
pickerView.frame=CGRectMake(0, 200, view.frame.width, 250)
} else if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
pickerView.frame=CGRectMake(0, 200, view.frame.width, 300)
}
// pickerView.backgroundColor = .whiteColor()
pickerView.showsSelectionIndicator = true
pickerView.delegate = self
pickerView.dataSource = self
self.pickerView = pickerView
let pickerToolBar = UIToolbar()
pickerToolBar.barStyle = UIBarStyle.Black //you can change the style
pickerToolBar.translucent = true
pickerToolBar.tintColor = UIColor.whiteColor() // or other colours
pickerToolBar.sizeToFit()
let spaceButtonPicker = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let cancelButtonPicker = UIBarButtonItem(title: "Ok", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelDatePicker:")
pickerToolBar.setItems([cancelButtonPicker, spaceButtonPicker], animated: false)
pickerToolBar.userInteractionEnabled = true
tfDateNaissance.inputAccessoryView = pickerToolBar
tfProjet.inputView = pickerView
tfProjet.inputAccessoryView = pickerToolBar
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func selectRow(row: Int, inComponent component: Int, animated: Bool){
}
func updatePicker(){
self.pickerView!.reloadAllComponents()
pickerView.selectRow(0, inComponent: 0, animated: false)
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if tfProjet.isFirstResponder(){
return pvProjetData.count
}
return 1
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if tfProjet.isFirstResponder(){
return pvProjetData[row]
}
return ""
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if tfProjet.isFirstResponder(){
let itemselected = pvProjetData[row]
tfProjet.text = itemselected
self.tfProjet.endEditing(true)
}
}
我看到按钮确定,但它不起作用,当我滑动时,行自动被选中。