标签:
1.代理反向传值
#import <UIKit/UIKit.h> //声明一个类 @class LHTableViewController; //声明一个协议 @protocol LHTableViewControllerDelegate <NSObject> //协议中的方法 -(void)LHTablieViewController:(LHTableViewController *)LHTablieViewController Color:(UIColor *)color; @end @interface LHTableViewController : UITableViewController //定义一个协议的变量 @property (nonatomic, assign)id<LHTableViewControllerDelegate> delegate; @end
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UIColor *color =datas[indexPath.row]; //判断是否响应代理方法(是否有其他的类遵守了这个协议) if ( [_delegate respondsToSelector:@selector(LHTablieViewController:Color:)] ) { //调用代理方法 [_delegate LHTablieViewController:self Color:color]; } [self.navigationController popToRootViewControllerAnimated:YES]; }
#pragma mark - LHTableViewController的代理方法 -(void)LHTablieViewController:(LHTableViewController *)LHTablieViewController Color:(UIColor *)color{ //给背景颜色赋值 self.view.backgroundColor = color; }
2.block反向传值
#import <UIKit/UIKit.h> //声明一个MyBlock的变量block typedef void(^Myblock)(UIColor *color); @interface LHTableViewController : UITableViewController @property(nonatomic,strong)Myblock block; @end
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UIColor *color =datas[indexPath.row]; // 调用 把block回转 if (_block) { _block(color); } [self.navigationController popToRootViewControllerAnimated:YES]; }
-(void)buttonClick:(UIButton *)button{ LHTableViewController *nc = [[LHTableViewController alloc]init]; [self.navigationController pushViewController:nc animated:YES]; nc.delegate = self; nc.block = ^(UIColor *color){ self.view.backgroundColor = color; }; }
标签:
原文地址:http://www.cnblogs.com/lijiehai/p/4399576.html