• 反向传值实例


    反向传值实例

    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;
        };
        
    }
    
  • 相关阅读:
    WCF+EntityFramework+mysql总结
    实现Win7远程桌面关机和重启
    EF 4.1 一些操作
    Ado.net利用反射执行SQL得到实体
    .net IL 指令速查
    VS2010 /VC/bin/rcdll.dll 无法找到资源编译器
    Win7下 httpRequest带证书请求https网站
    VS2010 自动关闭的问题解决方法
    Android 之 悬浮窗口
    论 Java 中获取一组不重复的随机数之性能问题
  • 原文地址:https://www.cnblogs.com/lijiehai/p/4399576.html
Copyright © 2020-2023  润新知