博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
委托模式精讲
阅读量:7164 次
发布时间:2019-06-29

本文共 2584 字,大约阅读时间需要 8 分钟。

      委托的作用有两个,一个是传值,一个是传事件。
      委托用到的最多的用途 回传值(回调)
      当我们声明了遵循的协议的属性时,属性的关键字要用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];

 

}

 

转载于:https://www.cnblogs.com/lcl15/p/4989966.html

你可能感兴趣的文章
Mysql: 利用Xtrabackup搭建从库
查看>>
云计算袭来 CIO是否感受到失业的危机
查看>>
【译】CSS 才不是什么黑魔法呢
查看>>
轻松把玩HttpClient之封装HttpClient工具类(六),封装输入参数,简化工具类
查看>>
JAVA线程间协作:Condition
查看>>
数据管理与数据丢失防护:区别何在?
查看>>
学习汇编的一些技巧和概念性常识
查看>>
AJAX请求也会重新刷新整个页面?
查看>>
苗凯翔:大数据是计算和存储融合的产物
查看>>
GITLAB-CI搭配Runner的使用
查看>>
编译Boost——Linux
查看>>
Timers Tutorial
查看>>
TensorFlow学习笔记之五——源码分析之最近算法
查看>>
中国移动云计算大会在苏州召开:大云4.0发布
查看>>
平衡企业管理与协作Worktile让工作更简单
查看>>
Java的三种代理模式简述
查看>>
物联网在工业革命中所担任的角色
查看>>
《中国人工智能学会通讯》——3.20 在线社会网络中的信息传播预测
查看>>
数据洪流驱动人工智能发展
查看>>
2017年Q2企业SaaS收入近150亿美元 同比增长31%
查看>>