In an attempt to Detect backspace in UITextField,
I've tried subclassing UITextField
and overriding -[UIKeyInput deleteBackward]
, but it never gets called. So, I'm suspecting UITextField
swizzles deleteBackward
to another method name.
Using the Objective-C Runtime, how can I determine which method name deleteBackward
has been swizzled to? Then, how can I change the implementation so that UITextField
will call [self.delegate textField:self shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""]
when the delete key is pressed when it's empty.
Also, will this kind of metaprogramming get my app rejected from the App Store?
Swizzling doesn't change the name of a method. If it did, it would be impossible to call the method, since the runtime finds the implementation using the name. All it does is change the address of the code which is run when you call a specific method.
My guess as to why the deleteBackward
method isn't being called is that the input system is using a method from the more complicated UITextInput protocol instead, most likely replaceRange:withText:
. Try swizzling that and performing your call when the text
argument is an empty string. Also make sure your swizzling function doesn't return an error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With