Block 简单使用


总是会忘记block的写法定义,记录几个常用的block.

@interface NextVC : UIViewController

@property(nonatomic,copy) void (^VoidBlack)(void);

@property(nonatomic,copy) void (^AgeBlock)(int age);

@property(nonatomic,copy) void (^PersonBlock)(NSString *person);

@end
- (IBAction)BackActions:(UIButton *)sender
{
    if (self.VoidBlack) {
        self.VoidBlack();
    }
    
    if (self.AgeBlock) {
        self.AgeBlock(23);
    }
    
    if (self.PersonBlock) {
        self.PersonBlock(@"PersonName");
    }
}
- (IBAction)JumpTwoVC:(UIButton *)sender
{
    NextVC *nextV = [[NextVC alloc]init];
    [self.navigationController pushViewController:nextV animated:YES];
    
    nextV.VoidBlack = ^{
      NSLog(@"OneBlock");
    };
    
    nextV.AgeBlock = ^(int age) {
        NSLog(@"Age = %i",age);
    };
}

相关