I\'m in a situation where need to call a class method from my view controller, have it do it\'s thing, but then perform some actions ONLY AFTER the class method has complete
You define the block as a custom type:
typedef void (^ButtonCompletionBlock)(int buttonIndex);
Then use it as an argument to a method:
+ (SomeButtonView*)buttonViewWithTitle:(NSString *)title
cancelAction:(ButtonCompletionBlock)cancelBlock
completionAction:(ButtonCompletionBlock)completionBlock
When calling this in code it is just like any other block:
[SomeButtonView buttonViewWithTitle:@"Title"
cancelAction:^(int buttonIndex) {
NSLog(@"User cancelled");
}
completionAction:^(int buttonIndex) {
NSLog(@"User tapped index %i", buttonIndex);
}];
If it comes time to trigger the block, simply call completionBlock() (where completionBlock is the name of your local copy of the block
I wrote a completionBlock for a class which will return the values of a dice after they have been shaked:
Define typedef with returnType (.h
above @interface
declaration)
typedef void (^CompleteDiceRolling)(NSInteger diceValue);
Define a @property
for the block (.h
)
@property (copy, nonatomic) CompleteDiceRolling completeDiceRolling;
Define a method with finishBlock
(.h
)
- (void)getDiceValueAfterSpin:(void (^)(NSInteger diceValue))finishBlock;
Insert previous defined method in .m
file and commit finishBlock
to @property
defined before
- (void)getDiceValueAfterSpin:(void (^)(NSInteger diceValue))finishBlock{
self.completeDiceRolling = finishBlock;
}
To trigger completionBlock
pass predefined variableType to it
(Don't forget to check whether the completionBlock
exists)
if( self.completeDiceRolling ){
self.completeDiceRolling(self.dieValue);
}
Regarding to http://goshdarnblocksyntax.com/
As a local variable:
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
As a property:
@property (nonatomic, copy) returnType (^blockName)(parameterTypes);
As a method parameter:
- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
As an argument to a method call:
[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
As a typedef:
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};
I always use this when I want to write a block:
http://fuckingblocksyntax.com
EDIT
If you are writing Swift then use this:
http://fuckingswiftblocksyntax.com
If the above links are not opening because of the obscene language, use this one.
http://goshdarnblocksyntax.com/
Simple Completion block
// Completion block method
-(void)myMethod:(void (^)(void))completion {
NSLog(@"iPhone");
completion();
NSLog(@"iPod");
}
// Calling completion block method
- (void)viewDidLoad {
[super viewDidLoad];
[self myMethod:^{
NSLog(@"iPad");
}];
}
// output
iPhone
iPad
iPod