0

Ok so Im creating a UIPickerView programmatically to trigger from a UITextField inside a TableCell(this may be a stupid idea but lets go with it for now anyway).

I refactored all the code for it into its own class to declutter my 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")

}

And so then I call in my UIViewController

var severitySection = SeveritySection()

self.severitySection.setPickerDelegate(self)
self.severitySection.createPickerToolbarwith(colour: UIColor.whiteColor())
self.severitySection.pickerToolbar.delegate = self
self.severitySection.setupTextfield()

but pressing the done button in the spinner toolbar won't fire any events. I tried setting the viewcontroller as the delegate to the UIToolbar, but that didnt change anything. I believe what I did was just refactor inline code in my view controller but I guess I missed something, or something implicitly defined is no longer there

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mike Li
  • 33
  • 6
  • 1
    change target to self not be nil in let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: nil,action: Selector("doneButtonAction")) – Huy Nghia Feb 24 '15 at 00:16
  • Do I need to make my class inherit from NSObject too? I did some digging elsewhere for an error message I got, I added that in and it seems to all work fine now? "Sibylv1[61919:5684143] *** NSForwarding: warning: object 0x7fc1d355e700 of class 'Sibylv1.SeveritySection' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[Sibylv1.SeveritySection doneButtonAction] (lldb) " – Mike Li Feb 24 '15 at 01:17
  • you should set target is class where you declared didButtonAction method – Huy Nghia Feb 24 '15 at 02:16
  • But does my swift target class need to be inherited from NSObject? – Mike Li Feb 24 '15 at 12:17
  • as error massage Sibylv1.SeveritySection not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[Sibylv1.SeveritySection doneButtonAction]. Is self.severitySection kind SeveritySection – Huy Nghia Feb 25 '15 at 00:05

1 Answers1

0

Echoing what Huy Nghia mentioned in the comments, if you set the target to nil, you've effectively send a message to a nil object, which does nothing.

[nil doneButtonAction] is effectively a no-op

Roderic Campbell
  • 719
  • 5
  • 14
  • So why was it fine when it was right inside the viewcontroller? – Mike Li Feb 24 '15 at 01:16
  • 1
    If you had the target set to nil, it didn't work. I promise you. There may have been an IBAction connected somewhere that got lost in the refactor for this posting, or, you didn't have the target as nil. – Roderic Campbell Feb 24 '15 at 02:00