美文网首页
iOS Block的使用

iOS Block的使用

作者: 麦兜兜买兜兜 | 来源:发表于2016-07-27 11:37 被阅读144次

    一 .最简单的block使用

    使用block的三个步骤:

    1.定义block变量
    2.创建block代码块
    3.调用block匿名函数

    定义一个block的构成包括:返回值,block名,参数类型。 
    block代码块作为一个匿名函数是可以被写在其他方法中的,所以一般我们将block代码块写在其他方法里,调用该方法的时候block代码块将不会被执行,只有回调block代码块的时候,才才会执行。

    ViewController.h

    import <UIKit/UIKit.h>
    @interface ViewController :  UIViewController@property(nonatomic,copy)void(^myBlock)(int);
    //1.声明一个block变量,直接将block声明成一个属性变量更方便实用。
    @end
    

    ViewController.m

    import "ViewController.h"
    import "NewViewController.h"
    @interface ViewController ()@end@implementation ViewController
    -(void)viewDidLoad {
        [super viewDidLoad]; 
    //注意:先加载写block代码块的方法[self blockTest];
    }
    -(void)blockTest{
    //2.在方法里面写block代码块
    _myBlock = ^(int a){
            NSLog(@"%d",a);
        };
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //3.调用block匿名函数_myBlock (10);
    NewViewController * newViewController = [[NewViewController alloc]init];
    newViewController.block = _myBlock;
    [self presentViewController:newViewController animated:YES completion:^{}];
    }
    @end
    

    上面代码,在ViewController里面实现了block实现的三个步骤,该block的功能就是打印参数值。
      
      touch方法实现从ViewController到NewViewController的跳转,同时将ViewController里面的block即myBlock赋给NewViewController里定义的block,使得两个block实质上成为同一个block,这样,在NewViewController里面就耶可以调用ViewController里的代码块了。

    NewViewController.h

    import <UIKit/UIKit.h>
    import "ViewController.h"
    @interface NewViewController : UIViewController@property(nonatomic,copy)void(^block)(int a);
    @end
    

    NewViewController.m

    import "NewViewController.h"
    @interface NewViewController ()
    @end
    
    @implementation NewViewController
    -(void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor grayColor];
    }
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    self.block(100);
    [self dismissViewControllerAnimated:YES completion:^{}];
    }
    @end
    

    二.block其他定义类型

    通过typedef声明block类型(因为block没有类型,用typedef声明一个类型)。如下两个声明了两个finishedblock类型的block。

    ViewController.h

    import <UIKit/UIKit.h>
    typedef int(^FinishedBlock)(int); 
    //此处FinishedBlock不是block变量,而是一种类型,即FinishedBlock类型。
    @interface ViewController : UIViewController
    {
    FinishedBlock block1; 
    //typedef声明一个FinishedBlock后,就可以用该类型定义block变量。
    }
    @property(nonatomic,copy) FinishedBlock block2;
    @end
    

    ViewController.m

    import "ViewController.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    
    -(void)viewDidLoad {
    [super viewDidLoad];
    
     block1 = ^(int a){
    NSLog(@"%d",a); 
    return a;
     };
    
    _block2 = ^(int a){
    NSLog(@"%d",a); 
    return a;
    };
    
    
    FinishedBlock block3 = ^(int a){
    NSLog(@"%d",a); 
    return a;
    };
    block3(30); //回调
    [self test];
    }
    
    -(void)test{
    //回调
     block1(10);
    _block2(20);
    }
    @end
    

    打印结果:
    2015-07-15 20:57:22.226 Test[618:21849] 30
    2015-07-15 20:57:22.227 Test[618:21849] 10
    2015-07-15 20:57:22.227 Test[618:21849] 20

    三.block作为参数传递

    ViewController.h

    import <UIKit/UIKit.h>
    typedef int(^FinishedBlock)(int);
    @interface ViewController : UIViewController
    @end
    

    ViewController.m

    import "ViewController.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    
    -(void)viewDidLoad {
    [super viewDidLoad]; 
    [self testBlock:^int(int a) {
    NSLog(@"%d",a);
     return a;
    }];
    }
    
    -(void)testBlock:(FinishedBlock)block{
      block(1000);//回调
    }
    @end
    

    相关文章

      网友评论

          本文标题:iOS Block的使用

          本文链接:https://www.haomeiwen.com/subject/mfsojttx.html