zoukankan      html  css  js  c++  java
  • 委托模式精讲

          委托的作用有两个,一个是传值,一个是传事件。
          委托用到的最多的用途 回传值(回调)
          当我们声明了遵循的协议的属性时,属性的关键字要用weak或者assign,目的是为了避免循环引用
          委托模式,它的特点是,一对一
          用途是用在有上下级关系的两个view,不能跨级调用
      例如:ReadViewController这个类里面是一个阅读小说的界面,现在有个需求:想改变小说的背景颜色和字体颜色,而它本身有不想做这件事,这是它就会找个代理SetViewController,帮他完成这一转变。
     
     
     
    #import "ReadViewController.h"

    #import "SetViewController.h"

     

    @interfaceReadViewController ()<colorDelegate>//ReadViewController这个类要遵循协议

     

    @end

     

    @implementation ReadViewController

     

    - (void)viewDidLoad {

        [superviewDidLoad];

        self.view.backgroundColor = [UIColorwhiteColor];

        self.title = @"阅读";

        

        UITextView *text = [[UITextView alloc]initWithFrame:self.view.bounds];

        text.userInteractionEnabled = NO;

        text.font = [UIFont systemFontOfSize:20];

        text.tag = 1;

        text.text = @"可不管对方可不可靠的放开保护开关迪佛如果认为是理科高考热死我也hi看认可是公开表示可认定为快高考人数为客户立刻同意和认可可以和客人可给他发可爱我的身高赶快回来你风格和空调管理和你了就会让他理解和内容与讨论哦豁咯快乐不过来后来通过联合厉害不人道了法国和老板了让他聊天如何管理法律的公会聊天聊天人干活了人聊天的好了不让他开会不反抗的效果不好开发过开放不会看人的反馈给好看热得快干活离开过人的可观可包括美国方面还";

        [self.view addSubview:text];

        

        UIBarButtonItem *item = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(edit )];

        self.navigationItem.rightBarButtonItem = item;

    }

    -(void) edit

    {

        SetViewController *setvc = [[SetViewControlleralloc]init];

        setvc.delegate = self;//

        [self.navigationControllerpushViewController:setvc animated:YES];

     

    }

    -(void) colorchange:(BOOL)sign//委托方法的实现

    {

        if (sign) {

            self.view.backgroundColor = [UIColorgrayColor];

            UITextView *te = (UITextView *) [self.view  viewWithTag:1];

              te.backgroundColor = [UIColor grayColor];

            te.textColor = [UIColor whiteColor];

        }

        else

        {

            self.view.backgroundColor = [UIColorwhiteColor];

            UITextView *te = (UITextView *) [self.view  viewWithTag:1];

             te.backgroundColor = [UIColor whiteColor];

            te.textColor = [UIColor grayColor];

        

        }

     

    }

    //////////////////////////////////////////////////////////////////////////////

    被委托方

    #import <UIKit/UIKit.h>

     

    @protocol colorDelegate <NSObject>  //声明一个协议

     

    -(void) colorchange:(BOOL) sign;

     

    @end

     

    @interface SetViewController : UIViewController

     

    @property (nonatomic ,weak) id<colorDelegate> delegate;//声明一个遵循协议的属性

     

    @end

     #import "SetViewController.h"

     

    @interfaceSetViewController ()

     

    @end

     

    @implementation SetViewController

     

    - (void)viewDidLoad {

        [superviewDidLoad];

        self.view.backgroundColor = [UIColorwhiteColor];

        

        UISwitch *s= [[UISwitchalloc]initWithFrame:CGRectMake(80, 90, 50, 10)];

        [s addTarget:selfaction:@selector(changcolor:) forControlEvents:UIControlEventValueChanged];

        [self.view addSubview:s];

    }

    -(void)changcolor:(UISwitch *) sw

    {

     

        if (sw.isOn)

            self.view.backgroundColor = [UIColorgrayColor];

            

       

        else

            self.view.backgroundColor = [UIColorwhiteColor];

        

        [_delegate colorchange:sw.isOn];

     

    }

  • 相关阅读:
    P3162 [CQOI2012]组装
    P3161 [CQOI2012]模拟工厂
    P3158 [CQOI2011]放棋子
    P3154 [CQOI2009]循环赛
    zabbix部署监控端(server)以及页面优化
    zabbix-agent端自定义监控项(free -m)服务器内存使用率
    java应用系统运行速度慢的解决方法
    at org.apache.hadoop.hbase.tmpl.master.BackupMasterStatusTmplImpl.renderNoFlush(BackupMasterStatusTm
    解决Hbase启动后,hmaster会在几秒钟后自动关闭(停掉)!!!
    全网最详细的Hadoop HA集群启动后,两个namenode都是standby的解决办法(图文详解)
  • 原文地址:https://www.cnblogs.com/lcl15/p/4989966.html
Copyright © 2011-2022 走看看