@propery(copy, nonatomic) void( ^ updateStatus)(NSString * , NSString * );
self.updateStatus(@“papa”, @“Mamma”);
__weak HelloViewController * weakSelf = self;
XmppPresence * persence = [
[xmppresence alloc] init];
presence.updateStatus = ^ (NSString * phone, NSString * status) {
weakSelf.friendUserName = phone;
};
int( ^ howMany)(int, int) = ^ (int a, int b) {
return a + b;
};
void( ^ justAMessage)(NSString * ) = ^ (NSString * message) {
NSLog(@
"%@", message);
};
void( ^ xyz)(void);
xyz = ^ (void) {
NSLog(@
"What's up, Doc?");
};@interface ViewController()@property(nonatomic, strong) NSString * ( ^ blockAsAMemberVar)(void);@end
on didload.._blockAsAMemberVar = ^ (void) {
return@
"This block is declared as a member variable!";
};
int factor = 5;
int( ^ newResult)(void) = ^ (void) {
return factor * 10;
};
NSLog(@
"%d", newResult());
NSDate * ( ^ today)(void);
today = ^ (void) {
return [NSDate date];
};
NSLog(@
"%@", today());
float results = ^ (float value1, float value2, float value3) {
return value1 * value2 * value3;
}(1.2, 3.4, 5.6);
NSLog(@
"%f", results);
A completion handler is the way (technique) for implementing callback functionality using blocks.
When showing a modal view controller, if you want to handle something after the view controller has been presented:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion NS_AVAILABLE_IOS(5_0);
[self presentViewController:viewController animated:YES completion:^{
NSLog(@"View Controller was presented...");
// Other code related to view controller presentation...
}];
Self method.
- (void)viewDidLoad
{
[self addNumber:5 withNumber:7 andCompletionHandler:^(int result) {
// We just log the result, no need to do anything else.
NSLog(@"The result is %d", result);
}];
}
-(void)addNumber:(int)number1 withNumber:(int)number2 andCompletionHandler:(void (^)(int result))completionHandler{
int result = number1 + number2;
completionHandler(result);
}