好的,所以我以编程方式创建一个 UIPickerView 以从 TableCell 内的 UITextField 触发(这可能是一个愚蠢的想法,但无论如何现在就让它继续吧)。
我将它的所有代码重构为它自己的类以整理我的 UIViewControler:
class SeveritySection {
let pickerToolbar: UIToolbar!
let picker: UIPickerView!
let textField: UITextField!
let tableCell: UITableViewCell!
var pickerOptions = Array<String>()
init() {
pickerToolbar = UIToolbar()
picker = UIPickerView()
tableCell = UITableViewCell()
textField = UITextField(frame: CGRectInset(tableCell.contentView.bounds, 15, 0))
}
func setOptions(optionsArray:Array<String>){
pickerOptions = optionsArray;
}
/// Initalises the toolbar with done on the rhs for picker
/// :param: colour of the toolbar
func createPickerToolbarwith(#colour:UIColor){
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: nil,action: Selector("doneButtonAction"))
let toolbarButtons = [flexSpace, doneButton];
pickerToolbar.sizeToFit()
pickerToolbar.setItems(toolbarButtons, animated: true)
pickerToolbar.backgroundColor = colour
}
func setupTextfield(){
textField.placeholder = "Choose Severity"
textField.inputView = picker
textField.inputAccessoryView = pickerToolbar
// self.severityText.keyboardType = UIKeyboardType.NumberPad
tableCell.addSubview(textField)
}
func setPickerDelegate(viewController: SinnerFormViewController){
picker.delegate = viewController
picker.dataSource = viewController
}
func setTextFieldTo(text:String){
self.textField.text = text
}
func doneButtonAction() {
textField.resignFirstResponder()
println("Done pressed")
}
然后我调用我的 UIViewController
var severitySection = SeveritySection()
self.severitySection.setPickerDelegate(self)
self.severitySection.createPickerToolbarwith(colour: UIColor.whiteColor())
self.severitySection.pickerToolbar.delegate = self
self.severitySection.setupTextfield()
但是按下微调器工具栏中的完成按钮不会触发任何事件。我尝试将视图控制器设置为 UIToolbar 的委托,但这并没有改变任何东西。我相信我所做的只是在我的视图控制器中重构内联代码,但我想我错过了一些东西,或者隐含定义的东西不再存在