码迷,mamicode.com
首页 > 其他好文 > 详细

反向传值实例

时间:2015-04-07 21:29:11      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

反向传值实例

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!