xamarin.ios - Detect backspace in UITextField in iOS8 -
for detecting backspace, i've overridden deletebackward
method (should work ios5+)
var input = new backspacetextfield(rectanglef.empty); etc input.becomefirstresponder();
here's code
public sealed class backspacetextfield : uitextfield { public backspacetextfield(rectanglef frame) : base(frame) { } public override void deletebackward () { console.writeline ("deletebackward"); } }
when press "backspace" button nothing happens. expect "deletebackward" message should appear
environment: ios8, xamarin
edit: 0
similar question on objective-c: detect backspace in uitextfield
i've done additional check. deletebackward
is method uikeyinput protocol, i've check inserttext
method, method works perfactly.
public override void inserttext (string text) { base.inserttext(text); }
i've checked deletebackward
on objective-c , works too.
do have ideas how detect backspace in uitextfield in ios8?
could please clarify why deletebackward
method not called?
edit: 1
i've submitted same question xamarin's forum. looks bug in ios8 + xamarin, because in ios 7.1 works perfactly.
it's bug. here's details
a lot of people have been saying bug, being problem still exists in gm i'm starting think might change in logic. said, wrote bit of code app , have tested on ios 7-8.
this code before red line of private api's, should have no problem using it. app code in app store.
add following method 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 little, calling super's implementation of method avoid losing inherited code. after going call -deletebackward
if there no text , ios version between 8-8.2.
edit: 1/28/15
it might helpful subclass -deletebackward method of subclassed uitextfield. fixes few conditional bugs. 1 being if use custom keyboard. heres example of 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 xamarin, original question asked 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": call deletebackward if field empty if(utils.isios8) { deletebackward(); return false; } return shoulddelete; }
verified on ios 7.1 , 8.1
edit: 4/14/15
as of ios 8.3 issue has been fixed. objective-c code has been updated reflect changes.
edit: 7/24/15
as @nischalhada commented, state exists text fields -textfield:shouldchangecharactersinrange:replacementstring:
called twice. problem exists on ios >= 8.3 while using custom keyboard. solution not ideal job , i'm not sure if there's other way. since both calls method performed on same run loop we'll use bool keep track of when execute code , dispatch async reset 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 method along internal ios // code. allowing changes on next run loop helps avoid issue. self.shouldtextfieldpreventchange = no; }); // work... } return toreturn; }
Comments
Post a Comment