Principle: The concept of autolock in C + + is realized by using the principle of local variable lifetime limited to the current function or the current code block, which is actually the concept of smart pointers.
The setenable of the button at the time of creation of the local variable is no, and setenable is yes at the end of the function without a block. If there is a block, you need to adjust the Blockit function, in fact, there is nothing to do, Just to let LLVM know that this variable will be released at the end of the block.
Header file
@interface d1autodisablebutton:nsobject{ uibutton* Sender;} Call the interface, declare a temporary variable, note that it cannot be global or self-member, and if you want to cross a function call, pass it as a parameter + (ID) Objectwithbutton: (uibutton*) obj;//do something in the block, Otherwise the variable will be released before the block executes.-(void) Blockit; @end
Realize
@implementation d1autodisablebutton-(ID) init{ [Super Doesnotrecognizeselector:_cmd]; return nil;} + (ID) Objectwithbutton: (uibutton*) obj{ d1autodisablebutton* lockobj = [[D1autodisablebutton alloc]initWithParam : obj]; return lockobj;} -(ID) Initwithparam: (uibutton*) obj{ sender = obj; [Sender setenabled:no];// NSLog (@ "Auto disable:%x", sender); return self;} -(void) dealloc{ [sender Setenabled:yes]; NSLog (@ "Auto enable:%x", sender);} -(void) blockit{ //nslog (@ "block call it");} @end
Using the Arc
Ways to prevent buttons from being clicked multiple times in iOS