UIBarButtonItem在另一个没有触发的类中创建的动作(swift)

时间:2015-02-24 00:15:28

标签: ios uipickerview resignfirstresponder uibuttonbaritem

好的,所以我以编程方式创建一个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()

但是按下微调器工具栏中的“完成”按钮不会触发任何事件。 我尝试将viewcontroller设置为UIToolbar的委托,但这并没有改变任何东西。我相信我所做的只是在我的视图控制器中重构内联代码,但我想我错过了一些东西,或隐含定义的东西不再存在

1 个答案:

答案 0 :(得分:0)

回应Huy Nghia在评论中提到的内容,如果你将目标设置为nil,你就可以有效地向一个没有任何内容的nil对象发送消息。

[nil doneButtonAction]实际上是无操作