Search code examples
iosdelegatesuitextfieldtarget-actionrubymotion

UITextField Target/Action vs Delegate Methods


I used to use XCode the develop my apps and using that; I could create a link up in IB/Storyboard that synthesized this methods:

- (void)addTarget:(id)target action:(SEL)action forControlEvents: (UIControlEvents)controlEvents

for when

However, no I'm doing it all programmatically (in rubymotion, though not necessarily relevant) and looking into the documentation for UITextField I found some delegate methods, which I implemented as follows:

def textFieldShouldReturn(text_field)
  text_field.resignFirstResponder
end

def textFieldShouldEndEditing(text_field)
  # this is for validations; don't set a first responder here
  true
end

def textFieldDidEndEditing(text_field)
  text_field == @email_field ? @password_field.becomeFirstResponder : delegate.validate_login_info
end

But using this, I found that I could not tab through the input fields (of which I have two; email and password) as I could before with the addTarget approach.

What is the difference between these two approaches? Does the former implement the latter behind the scenes? I don't understand why one will allow me to tab through and the other won't. Did I just implement the delegate methods differently than the target/action approach does behind the scenes?

Feedback appreciated, pachun


Solution

  • This has nothing to do with target/action. I am assuming you mean in Interface Builder you would drag from the UITextField to the File's Owner and select delegate from the HUD.

    Doing the above would have the effect of assigning the textField's delegate which you would need to do in code like this

    @email_field.delegate    = self
    @password_field.delegate = self
    

    the above is assuming that the class that creates the UITextField's will act as their delegate.