Adding a UIKit handler to an iOSTextField

  1. 5 months ago

    I am struggling getting to grips with declares and handlers to reach into UIKit events. I am trying to subclass an iOSTextField to add the ability to respond to the DidEndEditing and ShouldEndEditing events.

    What I have done so far is
    add two events to my iOSTextField

    DidEndEditing
    ShouldEndEditing

    I have then added two methods

    Sub RecDidEndEditing
    RaiseEvent DidEndEditing
    End Sub
    
    Function RecShouldEndEditing as Boolean
    return not RaiseEvent RecShouldEndEditing
    End Function 

    and finally have added a method which I intend to call in the open event of the iOSTextField

    Sub AttachHandlers
    Declare sub DidEndEditing Lib "UIKit" Selector "textFieldDidEndEditing" (callback as Ptr)
    AddHandler DidEndEditing, address of RecDidEndEditing
    Declare sub ShouldEndEditing Lib "UIKit" Selector "textFieldShouldEndEditing" (callback as Ptr) as boolean
    AddHandler ShouldEndEditing, address of RecDidEndEditing
    End Sub

    I have two issues.
    1 - I am pretty sure the callback as Ptr is wrong. I think this should be a pointer to my iOSTextField, but have become lost as to how I do that.
    2 - When I compile I get two errors on each line.
    a - Type mismatch error. Expected delegate Delegate(myClass), but got Delegate( )
    b - This method requires fewer parameters than were passed
    AddHandler DidEndEditing, addressof RecDidEndEditing

    and likewise for the second handler.

    Any assistance would be gratefully received.

  2. Ulrich B

    Jun 24 Pre-Release Testers, Xojo Pro Europe (Germany, Berlin) · xo...

    The methods you are trying to declare are not part of UITextField but of UITextFieldDelegate, a virtual class (a Protocol in Apple’s terms) that must be incorporated in a custom iOS subclass and connected to the control. You will find examples on that in UIKit or the remainders of AppleLib/iOSLib.

    There is a slightly easier method to approach this topic anyway: Both libraries above and some more examples you can find in this forum include an Xojo implementation of NSNotificationCenter and details on how that can be used to register for control’s Notifications.

    But sorry: Explanations for both would demand much more information than I like to fit into a forum thread on such a hot day as today. I hope mentioned examples will help a bit.

  3. Ulrich,

    Thanks, I will have a look as suggested.

    Mike

or Sign Up to reply!