Block 簡單使用

老是會忘記block的寫法定義,記錄幾個經常使用的block.atom

 

@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);
    };
}
相關文章
相關標籤/搜索