block相关

2015-12-14  本文已影响0人  必须这么打
-(void)viewDidLoad{
     [super viewDidLoad];
    
      void (^printBlock) () = ^(){
      //block 内部
       NSLog(我再block内部);
    };
      printBlock ();
}
int (^squer)(int) = ^(int num){
    return num*num;
};
NSLog (@"%d",squre(3));

x+y

int x =4;
square(3);
//__block 关键字 修饰 需要在block

__block int x =4;
void (^sum)(int) = ^(int y){
x += y;
NSLog(@"%d",x+y);
};
sum(100);

Block传值

.h 第二个页面

typedef void (^sendValue)(NSString *);
             //多个情况下 (NSString *,NSString*);
@property (nonatomic,assign) sedValue sv;

.m 第二个页面

-(IBAction)click:(id)seder{
[self dismissViewControllerAniamted:YES competin:^{
    _blockUpdateBtnTitle(@"传值");
  //多个 ^{ self.sv(@"传值",@"传值");
}];
}

.m 第一个页面

-(IBAction)jump:(id)sender{
SecondViewController *second = [[SecondViewController alloc] init];
second.sv = ^(NSString * str){
      //多个 ^(NSString * str,NSString *name)
      self.valueLabel.text = str;
//多个 self.valueLabel.text = [NSString stringWithFormat:@"%@ %@",str,name];
};
[self presentViewController:second animated:YES completion:nil];
}
上一篇 下一篇

猜你喜欢

热点阅读