zoukankan      html  css  js  c++  java
  • OC之Block的用法和实现委托

    • Block的基本概念
    • Block的基本用法
    • Block实现委托机制

    一、Block的基本概念

    Block是程序的代码块,这个代码块可以在需要的时候执行。IOS开发中,block到处可见,所以学好很重要

    二、Block的基本用法

    //
    //  main.m
    //  Block
    //
    //  Created by apple on 14-3-26.
    //  Copyright (c) 2014年 apple. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    
    //最基本的用法
    void test()
    {
        int (^Sum)(int, int) = ^(int a, int b)
        {
            return a + b;
        };
        
        
        NSLog(@"%i", Sum(10, 11));
    }
    
    
    //也是基本用法,另外Block代码段中可以使用外边的mul变量,但是不可以改变他的值,如果下改变他的值,可以使用__block来定义mul
    void test1()
    {
        int mul = 7;
        int (^myBlock)(int) = ^(int num)
        {
            return mul * num;
        };
        
        int c = myBlock(7);
        
        NSLog(@"%i", c);
    }
    
    //改变外边变量的值
    void test2()
    {
        __block int mul = 7;
        int (^myBlock)(int) = ^(int num)
        {
            mul = 10;
            NSLog(@"mul is %i", mul);
            return mul * num;
        };
        
        int c = myBlock(7);
        
        NSLog(@"%i", c);
    }
    
    //给block起一个别名,之后我们就可以任意命名的来使用block
    void test3()
    {
        typedef int (^MySum)(int, int);
        
        MySum sum = ^(int a, int b)
        {
            return a + b;
        };
        
        int c = sum(20, 39);
        
        NSLog(@"%i", c);
        
    }
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            test();
        }
        return 0;
    }

    OK,都有注释。在main函数中调用即可测试

    三、Block来实现委托模式

    //
    //  Button.h
    //  Block_Button
    //
    //  Created by apple on 14-3-26.
    //  Copyright (c) 2014年 apple. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    @class Button;
    
    //给block给一个别名ButtonBlock
    typedef void (^ButtonBlock)(Button *);
    
    @interface Button : NSObject
    
    //定义一个block的成员变量,暂时写成assign
    @property (nonatomic, assign) ButtonBlock block;
    
    -(void)click;
    
    @end
    //
    //  Button.m
    //  Block_Button
    //
    //  Created by apple on 14-3-26.
    //  Copyright (c) 2014年 apple. All rights reserved.
    //
    
    #import "Button.h"
    
    @implementation Button
    
    //模拟点击
    -(void)click
    {
        //回调函数,回调block
        _block(self);
    }
    
    @end
    //
    //  main.m
    //  Block_Button
    //
    //  Created by apple on 14-3-26.
    //  Copyright (c) 2014年 apple. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "Button.h"
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            
            Button *btn = [[[Button alloc] init] autorelease];
            
            //回调的函数
            btn.block = ^(Button *btn)
            {
                NSLog(@"%@被点击", btn);
            };
            
            [btn click];
            
        }
        return 0;
    }
  • 相关阅读:
    C#根据html生成PDF
    Oracle 存储过程异常处理
    Oracle事务之一:锁和隔离
    跨域解决方案一:使用CORS实现跨域
    AJAX POST&跨域 解决方案
    使用 jQuery Deferred 和 Promise 创建响应式应用程序
    jQuery:多个AJAX/JSON请求对应单个回调并行加载
    解决td标签上的position:relative属性在各浏览器中的兼容性问题
    盘点8种CSS实现垂直居中水平居中的绝对定位居中技术
    JQuery-UI Dialog下使用服务器端按钮失效
  • 原文地址:https://www.cnblogs.com/letougaozao/p/3626780.html
Copyright © 2011-2022 走看看