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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM