Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect backspace in UITextField in iOS8

For detecting the Backspace, I've overridden DeleteBackward method (should work with iOS5+)

var input = new BackspaceTextField(RectangleF.Empty);
etc
input.BecomeFirstResponder();

Here's the code

public sealed class BackspaceTextField : UITextField
{
    public BackspaceTextField(RectangleF frame) : base(frame)
    {
    }

    public override void DeleteBackward ()
    {
        Console.WriteLine ("DeleteBackward");
    }
}

When I press "Backspace" button nothing happens. I expect "DeleteBackward" message should appear

Environment: iOS8, xamarin

Edit: 0

Similar question on objective-c: Detect backspace in UITextField

I've done additional check. DeleteBackwardis method from UIKeyInput protocol, so I've check insertText method, this method works perfactly.

public override void InsertText (string text)
{
   base.InsertText(text);
}

I've checked deleteBackward on objective-c and it works perfectly too.

Do you have any ideas how to detect backspace in UITextField in iOS8?

Could you please clarify why the DeleteBackward method was not called?

Edit: 1

I've submitted the same question to the Xamarin's forum. Looks like a bug in iOS8 + xamarin, because in iOS 7.1 works perfactly.

It's a bug. Here's details

like image 712
GSerjo Avatar asked Aug 17 '14 23:08

GSerjo


2 Answers

A lot of people have been saying this is a bug, but being that this problem still exists in the GM I'm starting to think it might be a change in logic. With that said, I wrote this bit of code for my app and have tested it on iOS 7-8.

This code is slightly before the red line of private API's, however you should have no problem using it. My app with this code is in the app store.

Add the following method to your UITextField subclass.

- (BOOL)keyboardInputShouldDelete:(UITextField *)textField {
    BOOL shouldDelete = YES;

    if ([UITextField instancesRespondToSelector:_cmd]) {
        BOOL (*keyboardInputShouldDelete)(id, SEL, UITextField *) = (BOOL (*)(id, SEL, UITextField *))[UITextField instanceMethodForSelector:_cmd];

        if (keyboardInputShouldDelete) {
            shouldDelete = keyboardInputShouldDelete(self, _cmd, textField);
        }
    }

    BOOL isIos8 = ([[[UIDevice currentDevice] systemVersion] intValue] == 8);
    BOOL isLessThanIos8_3 = ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.3f);

    if (![textField.text length] && isIos8 && isLessThanIos8_3) {
        [self deleteBackward];
    }

    return shouldDelete;
}

To explain a little, were calling the super's implementation of this method to avoid losing inherited code. After were going to call -deleteBackward if there is no text and the iOS version is between 8-8.2.

EDIT: 1/28/15

It also might be helpful to subclass the -deleteBackward method of your subclassed UITextField. This fixes a few conditional bugs. One being if you use a custom keyboard. Heres an example of the method.

- (void)deleteBackward {
    BOOL shouldDismiss = [self.text length] == 0;

    [super deleteBackward];

    if (shouldDismiss) {
        if ([self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
            [self.delegate textField:self shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""];
        }
    }
}

EDIT: answer translated to Xamarin, as the original question asked this for Xamarin.

    [Preserve]
    [Export("keyboardInputShouldDelete:")]
    private bool KeyboardInputShouldDelete(UITextField textField)
    {
        var shouldDelete = true;

        if(RespondsToSelector(new Selector("_cmd")))
        {
            //Call base class
            shouldDelete = Messaging.bool_objc_msgSend_IntPtr(Handle, Selector.GetHandle("_cmd"), textField.Handle);
        }

        //ios8 "bug": always call DeleteBackward even if the field is empty
        if(Utils.IsIos8)
        {
            DeleteBackward();
            return false;
        }

        return shouldDelete;
    }

Verified on ios 7.1 and 8.1

EDIT: 4/14/15

As of iOS 8.3 this issue has been fixed. The Objective-C code has been updated to reflect the changes.

EDIT: 7/24/15

As @nischalhada commented, a state exists where the text fields -textField:shouldChangeCharactersInRange:replacementString: is called twice. The problem exists on iOS >= 8.3 while using a custom keyboard. My solution is not ideal but it does the job and I'm not sure if there's any other way. Since both calls to this method are performed on the same run loop we'll use a bool to keep track of when to execute the code and a dispatch async to reset the bool.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    BOOL toReturn = NO;

    if (!self.shouldTextFieldPreventChange) {
        self.shouldTextFieldPreventChange = YES;

        dispatch_async(dispatch_get_main_queue(), ^{
            // iOS8.3 custom keyboards might call this method along with internal iOS
            // code. Allowing changes on the next run loop helps avoid this issue.
            self.shouldTextFieldPreventChange = NO;
        });

        // do work...
    }

    return toReturn;
}
like image 192
cnotethegr8 Avatar answered Nov 11 '22 16:11

cnotethegr8


To be precise this is an Apple iOS8 bug. It's been reported a few times on Apple's developer forums. Sadly those bug reports are not publicly accessible so all we know is that it still happens with beta 5.

Side note: In general (99%), most of the Xamarin.iOS bindings are not specific for an iOS version (i.e. there's no version check) so different behaviour between iOS versions are often by design (documented) or Apple bugs (like this one).

like image 22
poupou Avatar answered Nov 11 '22 16:11

poupou