zoukankan      html  css  js  c++  java
  • oc中代理的简单运用

    今天和以为老同学聊了一些,深有感触,看它传值都是用代理写的,自己平时都是用block,通知之类的,基本不用代理,想想代理,感觉自己也有些模棱两可,所以动手写了一个代理简单运用的demo,写完之后思考了一番,在与block和通知做一些比较,豁然开朗,感觉自己在以后又多了一大助力。

    我一贯的态度,做项目的思路源于基础,我不奢求自己什么都会,只要求自己学的都能基本掌握原理,从而达到心到,手到,运用自如。

    关于代理之类的官方解释,我在此就不说了,不懂得,自己去查阅一些文档吧,这里只写它的原理和用法。

       代理不是类,只是一个方法声明的文件,先看它的创建方法

    file:///Users/mac/Desktop/01.png

    file:///Users/mac/Desktop/02.png

    代理文件中

    //
    //  ViewDelegate.h
    //  代理的简单应用
    //
    //  Created by mac on 16/3/9.
    //  Copyright © 2016年 WYP. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @protocol ViewDelegate <NSObject>
    
    - (void)creatViewInView;
    
    - (void)addSub:(NSString *)str;
    
    - (int)upSomething:(CGFloat)x;
    
    @end
    

     被委托方,签署代理,实现代理方法的一方

    //
    //  ViewController.m
    //  代理的简单应用
    //
    //  Created by mac on 16/3/9.
    //  Copyright © 2016年 WYP. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "SViewController.h"
    @interface ViewController ()<ViewDelegate>//类签署协议
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        SViewController *view = [[SViewController alloc] init];
    #warning 设置代理对象
        view.ViewDelegate = self;
        
        
    }
    #pragma mark ViewDelegate
    - (int)upSomething:(CGFloat)x{
        
        NSLog(@"x:%.1f",x);
        return 15;
    }
    - (void)addSub:(NSString *)str{
        
        NSLog(@"str:%@",str);
    }
    - (void)creatViewInView{
        
        NSLog(@"创建了");
    }
    
    @end
    

     委托方,调用代理方法的一方

    //
    //  SViewController.h
    //  代理的简单应用
    //
    //  Created by mac on 16/3/9.
    //  Copyright © 2016年 WYP. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import "ViewDelegate.h"//引入协议
    @interface SViewController : UIViewController
    //设置协议属性,用于决定代理方法什么时候执行
    @property (nonatomic, weak) id<ViewDelegate> ViewDelegate;
    
    @end
    
    //
    //  SViewController.m
    //  代理的简单应用
    //
    //  Created by mac on 16/3/9.
    //  Copyright © 2016年 WYP. All rights reserved.
    //
    
    #import "SViewController.h"
    
    @interface SViewController ()
    
    @end
    
    @implementation SViewController
    
    - (id)init{
        if (self = [super init]) {
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //            判断如果代理对象不存在,不执行代理方法
                if (!_ViewDelegate) {
                    
                    return ;
                }
    #pragma mark 调用代理中的方法
    //            无参数也无返回值的代理方法
                if ([_ViewDelegate respondsToSelector:@selector(creatViewInView)]) {
                    //外部判断,如果代理对象响应这个代理方法,才让其执行这个代理方法
                    [_ViewDelegate creatViewInView];
                    
                }
                
    //            有参数无返回值的代理方法
                if ([_ViewDelegate respondsToSelector:@selector(addObject:)]) {
                    
                    [_ViewDelegate addSub:@"zhang"];
                }
    
    //            有参数有返回值得代理方法
                if ([_ViewDelegate respondsToSelector:@selector(upSomething:)]) {
                    
                    int i = [_ViewDelegate upSomething:34.5];
                    NSLog(@"i=%d",i);
                    
                }
                
                
            });
        }
        return self;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
    }
    - (void)setViewDelegate:(id<ViewDelegate>)ViewDelegate{
        _ViewDelegate = ViewDelegate;
      
    }
    
    @end
    

  • 相关阅读:
    Python 特点
    Python简介
    数据库查询语句
    人月神话读书笔记01
    团队介绍
    团队项目一 原型展示+电梯演讲
    全球疫情可视化展示
    NABCD模型
    第六周学习进度
    构建之法阅读笔记03
  • 原文地址:https://www.cnblogs.com/zxh-iOS/p/5260190.html
Copyright © 2011-2022 走看看