ios开发 Block(二) 实现委托
2014-11-06 17:34
253 查看
委托和block是IOS上实现回调的两种机制。Block基本可以代替委托的功能,而且实现起来比较简洁,比较推荐能用block的地方不要用委托。
实现效果如图
第一步,自定义CustomCell
在custom.h文件里面有
@property (copy,nonatomic) void(^BuyGoods)(NSString *);
声明了一个block变量
在custom.m文件里面回调
NSString *goodName=_labName.text;
_BuyGoods(goodName);
下面看实现的类里面
在ViewController.m文件里面
__block NSString *tipMsg=cell.labName.text;
cell.BuyGoods=^(NSString *str){
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示内容" message:tipMsg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[alertView show];
};
实现Block里面的代码块,__block NSString *tipMsg=cell.labName.text;这一行保证变量能在block代码块里面使用.........
运行效果图:
实现效果如图
第一步,自定义CustomCell
#import <Foundation/Foundation.h> @interface CustomCell : UITableViewCell @property (strong, nonatomic) IBOutlet UILabel *labName; @property (copy,nonatomic) void(^BuyGoods)(NSString *); - (IBAction)ButtonBuyPressed:(id)sender; @end
#import "CustomCell.h" @implementation CustomCell - (IBAction)ButtonBuyPressed:(id)sender { NSString *goodName=_labName.text; _BuyGoods(goodName); } @end
在custom.h文件里面有
@property (copy,nonatomic) void(^BuyGoods)(NSString *);
声明了一个block变量
在custom.m文件里面回调
NSString *goodName=_labName.text;
_BuyGoods(goodName);
下面看实现的类里面
#import "ViewController.h" #import "CustomCell.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _tabGoods.delegate=self; _tabGoods.dataSource=self; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } cell.labName.text = [NSString stringWithFormat:@"商品名称%d",indexPath.row]; __block NSString *tipMsg=cell.labName.text; cell.BuyGoods=^(NSString *str){ UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示内容" message:tipMsg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil]; [alertView show]; }; return cell; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 10; } @end
在ViewController.m文件里面
__block NSString *tipMsg=cell.labName.text;
cell.BuyGoods=^(NSString *str){
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示内容" message:tipMsg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[alertView show];
};
实现Block里面的代码块,__block NSString *tipMsg=cell.labName.text;这一行保证变量能在block代码块里面使用.........
运行效果图:
相关文章推荐
- iOS开发那些事-iOS常用设计模式–委托模式案例实现
- iOS开发技巧系列---使用链式编程和Block来实现UIAlertView
- ((ios开发学习笔记))实现委托总结
- iOS开发 使用Block实现两个页面互相传值
- ios软件开发委托代理实现介绍
- iOS开发那些事-iOS常用设计模式–委托模式案例实现
- [IOS 开发] __block的用途和实现原理
- iOS开发之OC与swift开发混编教程,代理的相互调用,block的实现。OC调用Swift中的代理, OC调用Swift中的Block 闭包,swift 3.0
- iOS开发之用block来实现UIAlertView点击事件的代理方法
- IOS开发之路-Objective-C 代码块(Block)-block计算器的实现
- iOS开发之OC与swift开发混编教程,代理的相互调用,block的实现。OC调用Swift中的代理, OC调用Swift中的Block 闭包,swift 3.0
- iOS每日一记————————简单的实现委托 Block代码块 功能 和简单的自定义View
- IOS开发ObjectC Block的实现方式
- iOS开发那些事-iOS常用设计模式–委托模式案例实现 推荐
- iPhone开发之iOS 应用实现数据库文件拷贝
- iOS开发实现多任务机制
- iOS开发架构封装异步请求案例实现
- 【iOS-Cocos2d游戏开发之十五】详解CCProgressTimer 进度条并修改cocos2d源码实现“理想”游戏进度条!
- iOS开发_iphone 实现剪贴板操作_iphone 复制粘贴功能
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)! .