In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use dealloc, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Recently, there is a problem when encapsulating a UITextField classification. The general requirement is to encapsulate several functions of UITextField, so it is convenient for business to use it this way: because it is a system control, it is impossible to copy the dealloc method directly, so it needs some run-time magic.
Direct overwrite dealloc method
/ / limit the input length
[_ tfltv_limitLength:5]
/ / restrict input characters
[_ tfltv_limitContent: [NSCharacterSetWithCharactersInString: @ "- + *"]
/ / action triggered by matching input condition
[_ tfltv_matchCondition: ^ Bool (NSString*text) {
Return [textisEqualToString:@ "asd"]
} action: ^ (NSString*text) {
NSLog (@ "matchedasd")
}]
The basic implementation idea is to use a global singleton to notify the observer when the UITextField content changes, where the object parameter specifies the UITextField instance to be listened to. In this way, when the input content changes, the logic processing related to the corresponding UITextField instance can be triggered:
[[NSNotificationCenterdefaultCenter] addObserver: [Selfmanager] selector:@selector (textfieldDidChangedTextNotification:) name:UITextFieldTextDidChangeNotificationobject:target]
One problem with this approach is that when the UITextField instance is released, the corresponding notification needs to be removed. In other words, I need to listen to the release of the UITextField instance. There were two main ways of thinking at that time:
Cannot override dealloc method
Replace the dealloc method with hook. But dealloc is NSObjec's method, and if you want to hook this method, it will have an impact on all cocoa instances, and my actual goal is only UITextField, which is obviously not a good approach. And in fact, there is no direct hookdealloc method under ARC (which can be implemented at run time), resulting in a compilation error. Therefore, this kind of scheme Pass!
With AssociatedObject. We know that under ARC, when an instance is released, it also removes the strong reference to its instance variable. In this way, I can dynamically bind a custom helper object to the UITextField instance through AssociatedObject and listen for dealloc method calls to that helper object. Because according to my theory, when the UITextField instance is released, the only strong reference to the helper object is released, which will inevitably trigger the call to dealloc. This way, I can listen indirectly to the release of the host UITextField instance.
Thank you for reading this article carefully. I hope the article "how to use dealloc" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.