3

I want to perform some actions (reset search) when a textview's (x) is pressed.

Something like this: iPhone perform action when UITextField / UISearchBar's clear button is pressed but I don't know how to do it with Xamarin

I have this:

tbkSearchOrder.ShouldReturn += (textField) => { 
   ComboViewCtl.OnOrdersFiltered (this, tbkSearchOrder.Text);
   return true; 
};

which responds on the "Search" button pressed on the keyboard. UITextField has several methods named similar to that but they are methods, not events or actions or delegates. Ideally I'd avoid writing a big Delegate class for that text field.

Community
  • 1
  • 1
Sten Petrov
  • 10,943
  • 1
  • 41
  • 61

2 Answers2

8

They are methods because they actually return a value. C# Events are just a way to receive an event sent by the objet.

When an UITextField starts (the keyboard is shown and the object is ready to manage input events), the event Started is sent. You can use this one for clearing the UITextField

UITextField textField = new UITextField();
textField.Started += delegate {
     textField.Text = null;
};

For handling the tap on the clear button (this is sadly not an event, but the TextField is going to be cleared anyway if the delegate returns true):

UITextField textField = new UITextField();
textField.ShouldClear += delegate {
    // Insert the handling here
    return true;
};
rFlex
  • 460
  • 4
  • 16
  • I don't need to clear the text field, I need to reset the search filter when the text field is cleared – Sten Petrov Jul 22 '13 at 17:09
  • +1 Thank you! Apparently in the c# binding "ShouldClear" is a property (delegate) and not an event, so I've missed it while looking through the list – Sten Petrov Jul 22 '13 at 20:34
4
            this.sampleTextField1.ShouldReturn += (textField) => {
                sampleTextField2.BecomeFirstResponder ();
                textField.ResignFirstResponder ();
                return true;
            };

            this.sampleTextField1.ShouldBeginEditing += (textField) => {
                //Write here
                return true;
            };

            this.sampleTextField1.ShouldEndEditing += (textField) => {
                //Write here
                return true;
            };
            this.sampleTextField1.ShouldClear += (textField) => {
                //Write here
                return true;
            };

            this.sampleTextField1.ShouldChangeCharacters = (UITextField txt, NSRange range, string sampleTxt) => {
                var newLength = txt.Text.Length + sampleTxt.Length - range.Length;
                return newLength <= 9;

            };
Alvin George
  • 14,148
  • 92
  • 64
  • I don't see how is this answer an improvement on the answer I already accepted: much more code, irrelevant code (.Alpha = 1.0f), it shows the delegates but doesn't tell you where to handle the needed event. Very poor answer – Sten Petrov Mar 22 '16 at 21:58
  • @ Sten : The good practice is to add delegate to viewDidLoad ( this.enableUITextFieldDelegateMethods (); . I made it a separate method.) . Please have a look @ https://github.com/alvinreuben/BasicUI-Xamarin – Alvin George Mar 23 '16 at 05:07
  • There's still too much irrelevant junk to make this statement visible to the reader – Sten Petrov Mar 24 '16 at 18:38