代理传值:

//  SendViewController.h
#import <UIKit/UIKit.h>
@protocol SendInFor <NSObject>
-(void)sendInForIdea:(NSString*)text;
@end
@protocol SendInForTwo <NSObject>
-(void)sender:(NSString*)text;
@end @interface SendViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)id <SendInFor> delegate;
@property(nonatomic,retain)id <SendInForTwo> 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=; i<; 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 ;
} -(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(, , , )];
[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:]; //点击获取的 cell cell变色
// UITableViewCell *cell = [table cellForRowAtIndexPath:indexPath];
// cell.backgroundColor=[UIColor cyanColor];
}
}
-(void)pressBtn
{ [self.navigationController popViewControllerAnimated:YES]; }

具体方法

block反向传值:

//  FirstViewController.h

#import <UIKit/UIKit.h>
//直接回车,第一个参数是返回值类型,第二个为重定义的block名称,第三个为要传参数类型和参数名;然后需要定义重定义block类型的属性,并且实现参数为该重定义类型block的方法。
//typedef <#existing#> <#new#>;
//回调不加断点不容易找出错误
typedef void (^BlockColor)(UIColor *color);
typedef void (^BlockTitle)(NSString *title); @interface FirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
//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=; i<; 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: 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:@""];
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 ;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

具体方法

调用:

//  RootViewController.h
#import <UIKit/UIKit.h>
#import "SendViewController.h"
@interface RootViewController : UIViewController<SendInFor>
#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(, , , )];
[btn setBackgroundColor:[UIColor cyanColor]];
[btn setTitle:@"block传值" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside]; btn1=[UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(, , , )];
[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=@"代理传值";
}

代理和block反向传值的更多相关文章

  1. block 反向传值回调

    /** *  block 反向传值回调 */ //在第二个控制器中 //   (1)声明block,在基类中已写好 //   (2)写好传值方法 //(1) typedef void (^Return ...

  2. 代理和 block 传值的使用

    // // ZYViewController.h // BlockTest // // Created by yejiong on 14/11/2. // Copyright © 2014年 zzz. ...

  3. IOS Block 反向传值

    1.在需要像上一个界面传值的.h 文件实现代理方法 @property (nonatomic, copy) void(^isOpenHandler)(BOOL) ; 2.在执行操作的时候需要江操作的结 ...

  4. iOS中的代理和Block

    一.代理(Delegate) 1)含义 iOS中的代理,比如父母要去上班,到中午12点了,需要给宝宝喂饭吃,但是父母正在上班,这时需要有一个人来帮忙完成一些事情(需要有个保姆来帮忙给宝宝喂饭),此时, ...

  5. iOS 再谈 代理传值,block反向传值

    本贴的例子是:有A和B两个界面,要实现的效果就是先让A跳转到B,然后B中有个颜色的参数,当B跳转到A时,把这个颜色的参数传递给A,在A中利用这个颜色改变自己界面的颜色. 第1步:在发送者(界面B)中, ...

  6. 使用代理和block写一个alertView

    代理: MyAlertView.h: @property (nonatomic,assign)id delegate; @protocol MyAlertViewDelegate <NSObje ...

  7. Swift基础--通知,代理和block的使用抉择以及Swift中的代理

    什么时候用通知,什么时候用代理,什么时候用block 通知 : 两者关系层次太深,八竿子打不着的那种最适合用通知.因为层级结构深了,用代理要一层一层往下传递,代码结构就复杂了 代理 : 父子关系,监听 ...

  8. 使用block进行界面之间的反向传值

    目标:在两个独立的控制器的界面之间进行反向传值 关键技术:block 代码编写及运行环境:Xcode6.4 / 模拟器8.4 语言:Objective-C 注:使用纯代码实现,不使用xib/story ...

  9. iOS Block界面反向传值

    在上篇博客 <iOS Block简介> 中,侧重解析了 iOS Block的概念等,本文将侧重于它们在开发中的应用. Block是iOS4.0+ 和Mac OS X 10.6+ 引进的对C ...

随机推荐

  1. Snmp配置

    http://www.07net01.com/linux/CentOSxiaSNMPfuwuanzhuang_496848_1372907142.html

  2. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem G: Check The Check(模拟国际象棋)

    Problem G: Check The Check Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 10  Solved: 3[Submit][Statu ...

  3. OracleDBConsoleorcl是具体管什么的服务(转)

    这个服务是oracle EM的就是oracle企业管理器这个工具可以通过网页的方式监控数据库,对数据库参数等做修改.里面还有oracle本身对于当前系统内存,SQL,参数等的建议.DBA可以根绝这些建 ...

  4. Android的ListView详解

    在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示.抽空把对ListView的使用做了整理,并写了个小例子,如下图. 列表的显示需要三 ...

  5. 模拟 CSU 1562 Fun House

    题目传送门 /* 题意:光线从 '*' 发射,遇到 '/' 或 '\' 进行反射,最后射到墙上,将 'x' 变成 '&' 模拟:仔细读题,搞清楚要做什么,就是i,j的移动,直到撞到墙,模拟一下 ...

  6. bug记录

    1>-[DYMessageNewsTableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (37 ...

  7. Android 蓝牙 BLE 开发笔记

    最近公司头戴换了一块蓝牙4.0 BLE模块,所以我们Android组要适配 BLE.Android BLE 需要 4.3 以上系统,api 还是非常简单的, 第一步就是扫描, 扫描到设备后就可以连接了 ...

  8. BZOJ4123 : [Baltic2015]Hacker

    黑掉的一定是一个长度为$\lfloor\frac{n+1}{2}\rfloor$的区间. 于是枚举初始点,然后查询包含它的区间的最小值. 通过维护前后缀最小值+单调队列$O(n)$解决. #inclu ...

  9. BZOJ3790 : 神奇项链

    Manacher求出所有极长回文子串后,得到一堆线段,转化成线段覆盖问题 预处理出g[i]表示左端点不超过i的右端点的最大值 贪心地线段覆盖即可 时间复杂度$O(n)$ #include<cst ...

  10. HDU 5087 (线性DP+次大LIS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目大意:求次大LIS的长度.注意两个长度相同的LIS大小比较,下标和大的LIS较大. 解题思 ...