selector parameters

While attempting to use the action arguement of the constructor of a UIButtonBarItem

var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: textField, action:"textFieldShouldReturn:textField")


I have tried a number of variations on this theme...

target: textField, action:"textFieldShouldReturn:textField:"

target: self, action:"textFieldShouldReturn:textField"

etc...

(textField is defined as a UITextField)


I get the following error at runtime:

[UITextField textFieldShouldReturn:textField]: unrecognized selector sent to instance


The target method should have the correct signature but I don't understand why the selector does not find the method.


func textFieldShouldReturn(textField: UITextField) -> Bool { //delegate method

Hopefully this is something simple that I am missing.

please be gentle... this is my first time 🙂

cheers

/chossy

Answered by OOPer in 35910022

Usually, calling UITextFieldDelegate method from your app code explicitly has no meaning. The method is called from within the UITextField, to customize its default behavior.


I guess you need and want to do something like this:

To setup UIBarButtonItem:

        var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action:"doneAction:")

And in the same class, declare the action method:

    func doneAction(sender: AnyObject) {
        //Do something for textField
    }


You need to fill up `Do something for textField`, with what you actually intend to do.

func textFieldShouldReturn(textField: UITextField) -> Bool { //delegate method

Selector for the method above is Selector("textFieldShouldReturn:"), the first parameter does not have an external name and it is not included in the selector.

(Of course, as you tried, in many cases enclosing with Selector( ) is not needed.)


Sometimes you need to add @objc annotation to the method to make it accessible with its selector, but maybe this is not the case.

var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: textField, action:"textFieldShouldReturn:")

returns this at runtime:

[UITextField textFieldShouldReturn:]: unrecognized selector sent to instance

I think it has something to do with -what- is being passed.

then again, I'm asking for help so...


are there some rules that I may be ignorant of when passing parameters to these conjured method references? I'm still fuzzy on how the compiler gets from a "string" to a spot in memory to execute.

Thank you for the help. 🙂

I was missing your target set to textField. UITextField does not have textFieldShouldReturn: method, UITextFieldDelegate does.

If your view controller is being the UITextFieldDelegate and your code is written in the view controller, target: self will work.


Your code needs too much guess, I was missing another thing. Updating.

ah. ok, I see.

Now, I get into the correct method but the passed object is a UIBarButtonItem and not the UITextField.

Why is that? is there a way to explicitly tell it what parameter to pass?


I 'm sorry to impose, but I cannot seem to find documentation spells this out clearly for me.


cheers

/chossy

Accepted Answer

Usually, calling UITextFieldDelegate method from your app code explicitly has no meaning. The method is called from within the UITextField, to customize its default behavior.


I guess you need and want to do something like this:

To setup UIBarButtonItem:

        var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action:"doneAction:")

And in the same class, declare the action method:

    func doneAction(sender: AnyObject) {
        //Do something for textField
    }


You need to fill up `Do something for textField`, with what you actually intend to do.

Not sure what you are doing here. What is the purpose of the text field? The action method should be a simple method that gets called when the user taps the bar button.


A textFieldShouldReturn: method is only called when the user taps the return key on the keyboard while in an edit field.

Thank you very much for your time and thought on this.

I was hoping for something more elegant but I can see now that I'll have to go the long way around as they say.


once again. thank you so much for your time.


cheers

/chossy

selector parameters
 
 
Q