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];

     

    }

  • 相关阅读:
    OpenGL ES学习001---绘制三角形
    Mac关机时处于黑屏状态
    静态变量和实例变量的区别(配图解释专业术语,通俗易懂)
    用shape画内圆外方,形成一个圆形头像
    最全的敏捷认证对比(CSM/PMI-ACP/EXIN)
    Certified Scrum Master CSM 中文资料大全
    如何打造优秀的远程敏捷团队(9步)
    博客搬家
    权威的国际敏捷认证Certified Scrum Master (CSM)
    博客园入驻了
  • 原文地址:https://www.cnblogs.com/lcl15/p/4989966.html
Copyright © 2011-2022 走看看