Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Runtime: Swizzled method name?

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?

like image 426
ma11hew28 Avatar asked Oct 20 '11 16:10

ma11hew28


1 Answers

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.

like image 99
ughoavgfhw Avatar answered Oct 23 '22 19:10

ughoavgfhw