博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
代理和block反向传值
阅读量:6058 次
发布时间:2019-06-20

本文共 8149 字,大约阅读时间需要 27 分钟。

代理传值:

//  SendViewController.h#import 
@protocol SendInFor
-(void)sendInForIdea:(NSString*)text;@end@protocol SendInForTwo
-(void)sender:(NSString*)text;@end@interface SendViewController : UIViewController
@property(nonatomic,retain)id
delegate;@property(nonatomic,retain)id
delegateB;@property(nonatomic,retain)NSIndexPath*indexPath;@e代理
代理方法
#import "SendViewController.h"//代理传值@interface SendViewController (){    UITableView*table;    NSMutableArray*data;}@end@implementation SendViewController- (void)viewDidLoad {    [super viewDidLoad];    data=[[NSMutableArray alloc]init];    table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];    table.delegate=self;    table.dataSource=self;    [self.view addSubview:table];    for (int i=0; i<20; i++) {                NSString*string=[[NSString alloc]initWithFormat:@"数据%d",i];        [data addObject:string];    }}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [data count];}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{        return 60;}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *str=@"str";    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:str];    if (cell==nil) {        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];    }//    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];    UIImageView *norImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];    [norImage setImage:[UIImage imageNamed:@"image"]];    cell.textLabel.text=[data objectAtIndex:indexPath.row];    //点击单元格 变换背景图片    if ([_indexPath isEqual: indexPath]) {                cell.accessoryType = UITableViewCellAccessoryCheckmark;        cell.backgroundColor=[UIColor redColor];        cell.accessoryView=norImage ;       /* 可以自定义图片  按钮 文本        cell.accessoryType=btn;        cell.accessoryType=label;         */    }        else{        cell.accessoryType = UITableViewCellAccessoryNone;        cell.backgroundColor=[UIColor whiteColor];        cell.accessoryView=nil ;    }    return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    if ([self.delegate respondsToSelector:@selector(sendInForIdea:)]) {                _indexPath = indexPath;        [self.delegate sendInForIdea:[data objectAtIndex:indexPath.row]];        [table reloadData];        //      cell.imageView.image=[UIImage imageNamed:@"2"];        //延迟操作     延迟跳转时间       [self performSelector:@selector(pressBtn) withObject:nil afterDelay:3];          //点击获取的 cell  cell变色//    UITableViewCell *cell = [table cellForRowAtIndexPath:indexPath];//    cell.backgroundColor=[UIColor cyanColor];    }}-(void)pressBtn{     [self.navigationController popViewControllerAnimated:YES];}
具体方法

block反向传值:

//  FirstViewController.h#import 
//直接回车,第一个参数是返回值类型,第二个为重定义的block名称,第三个为要传参数类型和参数名;然后需要定义重定义block类型的属性,并且实现参数为该重定义类型block的方法。//typedef <#existing#> <#new#>;//回调不加断点不容易找出错误typedef void (^BlockColor)(UIColor *color);typedef void (^BlockTitle)(NSString *title);@interface FirstViewController : UIViewController
//block设置为属性,修饰符用copy,即使使用strong,编译器也会将strong处理成copy@property(nonatomic,copy)BlockColor color;@property(nonatomic,copy)BlockTitle Title;@endblock方法
block方法
//  FirstViewController.m#import "FirstViewController.h"#import "TableViewCell.h"//block 反向传值 @interface FirstViewController (){    UITableView *table;    NSMutableArray *data;    NSMutableArray *arr;}@end@implementation FirstViewController- (void)viewDidLoad {    [super viewDidLoad];    arr =[[NSMutableArray alloc]init];    for (int i=0; i<20; i++) {        NSString * str=[[NSString alloc]initWithFormat:@"数据%d",i];        [arr addObject:str];    }        table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];    table.dataSource=self;    table.delegate=self;    [self.view addSubview:table];    }-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [arr count];}-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    TableViewCell *customCell = (TableViewCell *)cell;    [UIView animateWithDuration:2 animations:^{        customCell.image.alpha = 1.0f;    }];}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    TableViewCell *cell=[[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil] lastObject];    cell.Title.text=[arr objectAtIndex:indexPath.row];        //xib 中button不要设置点击事件   这样可以在cell里面调取按钮(点击单元格的按钮可以跳转)    cell.button.tag=indexPath.row;    [cell.button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];    cell.image.image =[UIImage imageNamed:@"2"];    cell.image.alpha =0.2f;    return cell;    }- (void)clickAction:(UIButton *)sender{    NSInteger tag=sender.tag;    NSLog(@"block 传值");   // __weak typeof(self) weakSelf=self;    if (_Title) {        _Title([arr objectAtIndex:tag]);    }    if (_color) {        _color([UIColor redColor]);    }    [self.navigationController popToRootViewControllerAnimated:YES];}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"hello world");       TableViewCell *cell = [table cellForRowAtIndexPath:indexPath];    NSString *string = arr[indexPath.row];    NSLog(@"string=%@",string);}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 70;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}
具体方法

 

调用:

//  RootViewController.h#import 
#import "SendViewController.h"@interface RootViewController : UIViewController
#import "RootViewController.h"#import "FirstViewController.h"#import "SendViewController.h"@interface RootViewController () {    FirstViewController *first;    SendViewController *send;    UIButton *btn;    UIButton *btn1;}@end@implementation RootViewController- (void)viewDidLoad {    [super viewDidLoad];    self.navigationItem.title=@"test";    first =[[FirstViewController alloc]init];    send=[[SendViewController alloc]init];    send.delegate=self;    [self creatBtn];}-(void)creatBtn{    btn=[UIButton buttonWithType:UIButtonTypeCustom];    [btn setFrame:CGRectMake(50, 100, 100, 100)];    [btn setBackgroundColor:[UIColor cyanColor]];    [btn setTitle:@"block传值" forState:UIControlStateNormal];    [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];        btn1=[UIButton buttonWithType:UIButtonTypeCustom];    [btn1 setFrame:CGRectMake(200, 100, 100, 100)];    [btn1 setBackgroundColor:[UIColor cyanColor]];    [btn1 setTitle:@"代理传值" forState:UIControlStateNormal];    [btn1 addTarget:self action:@selector(pressBtn1) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];    [self.view addSubview:btn1];  }-(void)pressBtn{    //int a = 0;   block 里面不能改变a的值(报错,block下面打印原来的值),但是可以将a的值赋给别人    //弱引用属性    //弱引用 self    __block __weak typeof(self) weakSelf = self;    __block __weak UIButton *_button = btn;    first.color=^(UIColor *color){    _button.backgroundColor=color;        };        first.Title=^(NSString *title){    weakSelf.navigationItem.title=@"block传值";    //强引用   // [btn setTitle:title forState:UIControlStateNormal];    //弱引用    [_button setTitle:title forState:UIControlStateNormal];    };    [self.navigationController pushViewController:first animated:YES];}-(void)pressBtn1{    [self.navigationController pushViewController:send animated:YES];}-(void)sendInForIdea:(NSString *)text{    [btn1 setTitle:text forState:UIControlStateNormal];    self.navigationItem.title=@"代理传值";}

 

 

转载于:https://www.cnblogs.com/sayimba/p/5662493.html

你可能感兴趣的文章
如何分析并构造cacheurl正则实现视频缓存
查看>>
终于理解你的软件 搞那么多年了 (通用权限管理系统组件源码完善了7-8年)
查看>>
清除Win7的SID工具Sysprep
查看>>
zabbix template system.cpu.load[percpu,avg1] not suppoted
查看>>
50个必备的实用jQuery代码段(转)
查看>>
缺陷的定义级别、优先级及状态
查看>>
Linux操作系统的常用命令
查看>>
ios开发工程师常见面试题汇总
查看>>
mysql错误
查看>>
13th Dec, 2011 决定退学了
查看>>
gpg: 无法检查签名:找不到公钥
查看>>
Mysql给用户设置权限
查看>>
MongoDB的Limit方法返回数据
查看>>
查看Python中的保留字
查看>>
icinga-web 安装
查看>>
让笔记本电池更耐用的节能设置技巧
查看>>
8.JSP与JavaBean
查看>>
【Java例题】1.4圆类
查看>>
keepalived+lvs
查看>>
卸载ESXi后,GPT删除办法
查看>>