Objective-C运行时:Swizzled方法名?

时间:2023-01-15 23:06:17

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.

在试图检测UITextField中的backspace时,我尝试了子类化UITextField和重写-[UIKeyInput deleteback],但它从未被调用。所以,我怀疑UITextField swizzles删除到另一个方法名。

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.

使用Objective-C运行时,如何确定哪个方法名deleteback被swizzled到?然后,如何更改实现,以便当删除键为空时按下delete键时,UITextField将调用[self.delegate textField:self shouldChangeCharactersInRange:NSMakeRange(0,0) replacementString:@"]。

Also, will this kind of metaprogramming get my app rejected from the App Store?

同样,这种元编程会不会让我的应用程序被app Store拒绝?

1 个解决方案

#1


3  

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.

我的猜测是,之所以没有调用deleteback方法,是因为输入系统使用的是更复杂的UITextInput协议中的方法,很可能是replaceRange:withText:。尝试在文本参数为空字符串时刷新它并执行调用。也要确保你的旋转函数不会返回错误。

#1


3  

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.

我的猜测是,之所以没有调用deleteback方法,是因为输入系统使用的是更复杂的UITextInput协议中的方法,很可能是replaceRange:withText:。尝试在文本参数为空字符串时刷新它并执行调用。也要确保你的旋转函数不会返回错误。