zoukankan      html  css  js  c++  java
  • 反向传值实例

    反向传值实例

    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;
        };
        
    }
    
  • 相关阅读:
    利用JS实现的根据经纬度计算地球上两点之间的距离
    html中meta标签作用详解
    Jquery Highcharts 参数配置说明
    Properties类的使用示例
    利用数据库模版创建方便部署的.Net项目调试环境
    ASP.NET修改Web.Config文件(对xml的操作)
    创建动态数据输入用户界面
    DataGrid相关知识总结(收集自各位老大处)
    在C#.net中如何操作XML
    C#中使用反射的性能分析
  • 原文地址:https://www.cnblogs.com/lijiehai/p/4399576.html
Copyright © 2011-2022 走看看