Block

传值,只能从后往前传,需要三部

..RootViewController.m 准备工作
先写用来实现功能的block

void(^newBlock)(NSString *)=^(NSString *str){
        NSLog(@"%@",str);
    };

1.要把block的遥控器从前往后通过属性进行传值,SecondViewController.h定义一个属性接受

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

2.第二步,从前往后传block

RootViewController.m

   secVC.newBlock=newBlock;

3.SecondViewController.m调用block

-(void)click:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];
    [self.deleage changeColor];

#pragma mark 第三步,调用block
 self.newBlock(self.textField.text);
}

具体用法如下:
SecondViewController.h

#import <UIKit/UIKit.h>

@protocol SecondViewControllerDeleage <NSObject>

-(void)changeColor;

@end

@interface SecondViewController : UIViewController
@property(nonatomic,retain)id<SecondViewControllerDeleage>deleage;

#pragma mark block第一步,要把block的遥控器从前往后通过属性进行传值
@property(nonatomic,copy)void(^block)();

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


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

@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()
@property(nonatomic,retain)UIButton *button;
@property(nonatomic,retain)UITextField *textField;
@end

@implementation SecondViewController
-(void)dealloc{
    Block_release(_nameblock);
    Block_release(_block);
    Block_release(_newBlock);
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor orangeColor];
    self.textField=[[UITextField alloc] initWithFrame:CGRectMake(100, 300, 150, 100)];
    [self.view addSubview:self.textField];
    self.textField.layer.borderWidth=1;
    self.textField.layer.cornerRadius=10;
    self.button=[UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.button];
    self.button.frame=CGRectMake(100, 100, 150, 100);
    self.button.layer.cornerRadius=10;
    self.button.layer.borderWidth=1;
    [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitle:@"上一页" forState:UIControlStateNormal];

}
-(void)click:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];
    [self.deleage changeColor];

#pragma mark 第三步,调用block
//    self.block();

//    self.newBlock(self.textField.text);
    self.nameblock(self.textField.text);
}

RootViewController.m

#import "RootViewController.h"
#import "SecondViewController.h"

@interface RootViewController ()<SecondViewControllerDeleage,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UIButton *button;
@property(nonatomic,retain)UITextField *textField;
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)UITableView *tableView;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor cyanColor];
    self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松", @"徐宁", @"张清", @"杨志", @"董平", @"索超", @"戴宗", @"刘唐", @"李逵", @"史进", @"穆弘", @"雷横", @"李俊",nil];
    self.tableView=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];



//    int (*p)(int ,int )=maxValue;
    //block分为两部分
    //左边
    //以返回值类型作为开头,
    //后接一个小括号,里面写^+block的名,
    //最后的小括号里写参数类型,没有就不写

    //右边
    //^作为开头
    //小括号里写形参的列表
    //大括号写函数实现的部分
    //以分号作为结尾
//    void (^block)() = ^(){
//        NSLog(@"等等");
//    };
//    
//    //block调用
//    block();

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.textLabel.text=self.arr[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondViewController *secVC=[[SecondViewController alloc] init];
    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];
    void(^nameblock)(NSString *)=^(NSString *str){
        [self.arr addObject:str];
        [self.tableView reloadData];
    };
    secVC.nameblock=nameblock;

}


-(void)click:(UIButton *)button{
    SecondViewController *secVC=[[SecondViewController alloc] init];
    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];
//    secVC.deleage=self;

    //先写用来实现功能的block
//    void(^block)()=^(){
//        self.view.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
//    };
//#pragma mark 第二步,从前往后传block
//    secVC.block=block;
#warning 受到里面形参范围的影响,只能在大括号里用,所以以后从后向前进行传值的时候,所有操作都写在block里
    void(^newBlock)(NSString *)=^(NSString *str){
        NSLog(@"%@",str);
    };
    secVC.newBlock=newBlock;

}
03-08
### Block在编程中的含义和用法 #### 区块链上下文中的Block 在区块链技术领域,`block`通常指代构成区块链的基本单元。每个区块包含了若干交易记录和其他元数据。具体来说,在智能合约环境中获取当前区块信息可以通过定义一个名为 `get_block_info()` 的函数实现: ```solidity contract BlockInfo { function getBlockInfo() public view returns (uint, uint, uint, address) { return ( block.number, block.timestamp, block.difficulty, block.coinbase ); } } ``` 上述代码展示了如何在一个 Solidity 合约中创建一个可以查询当前区块详情的方法[^1]。 #### NumPy 中的block 方法 对于 Python 编程语言而言,特别是在处理数值运算时经常使用的库——NumPy 提供了一个叫做 `block` 的方法。此方法主要用于构建由其他数组组成的大型数组结构。下面是一个简单的例子来说明其基本用途: ```python import numpy as np arr = np.block([ [np.array([1, 2]), np.array([3])], [np.eye(2), [[4]]] ]) print(arr) ``` 这段脚本会输出如下矩阵: ``` [[1. 2. 3.] [0. 1. 4.]] ``` 这里利用了 `block` 函数将几个不同的子阵列拼接在一起形成一个新的二维数组[^2]。 #### Linux 网络编程中的非阻塞连接尝试 当涉及到操作系统层面的操作时,“block”也可能指的是程序执行过程中是否会暂停等待某个事件的发生。例如在网络通信场景下建立TCP连接的过程中可以选择采用非阻塞模式来进行快速失败检测: ```c int connect_nonblock(const char *lpIP, int nPort){ int err = -1; socklen_t len = sizeof(err); // ...省略部分初始化套接字并设置为非阻塞模式的代码... if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0){ if(errno==EINPROGRESS){ printf("Non-blocking connection attempt started.\n"); return 0; /* 连接正在尝试 */ }else{ perror("connect error"); close(sockfd); return -1; } } // 成功立即返回或进一步处理已完成的连接... } ``` 在这个 C 语言片段里,通过调用 `connect()` 并配合特定错误码判断实现了异步发起网络请求的功能[^3]。 综上所述,尽管都涉及到了“block”,但在不同语境和技术栈之间其实有着截然不同的意义与应用场景。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值