1 typedef void (^CallBack)(int index); 2 3 @property(nonatomic, copy)CallBack callBack;
1 - (IBAction)click2:(id)sender 2 { 3 if(self.callBack) 4 self.callBack(2); 5 } 6 7 8 - (IBAction)click3:(id)sender 9 { 10 if(self.callBack) 11 self.callBack(3); 12 }
1 SecondViewController *secondController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; 2 3 secondController.callBack = ^(int index) 4 { 5 self.clickBtnLabel.text = [NSString stringWithFormat:@"click %d", index]; 6 }; 7 8 [self.navigationController pushViewController:secondController animated:YES];
- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block;
简单举个例子,我们要在字典中根据key来查找某个value,然后把value记录下来。
1 NSArray *keyArray = @[@"aa", @"ddd", @"cc", @"bb", @"ww", @"111"]; 2 NSArray *valueArray = @[@"apple", @"ios", @"mac", @"xcode", @"view", @"array"]; 3 NSDictionary *enumDict = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray]; 4 __block NSString *valueString = nil; 5 [enumDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){ 6 if([key isEqualToString:@"bb"]) 7 { 8 valueString = obj; 9 *stop = YES; 10 } 11 }];
整个过程变的简单了。
还有在实现UIView动画的时候,block能让我们更加简单高效的实现很多特效。
1 [UIView animateWithDuration:0.5 animations:^{ 2 animateView.alpha = 0.0; 3 } completion:^(BOOL finished) { 4 animateView.alpha = 1.0; 5 }];
这段代码实现了view透明度从1.0变到完全透明,用时0.5秒,当动画结束后重新显示view。
原文地址:http://www.cnblogs.com/yulang314/p/3708072.html