zoukankan      html  css  js  c++  java
  • 关于delegate 与 protocol 的理解 iOS

    delegate  protocol 是objective-c 语法的一部分 但他们两个却完全不是一回事。主要是我们经常在同一个文件里见到这两个东西

    protocol(协议)我的理解就是定义这么一个东西。以后就按这里的规定来办事。

    delegate(委托)  就是把事情委托给别人去办 

    @required 就是必须去办的。比如UITableView  delegate里面的:

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    这个就是必须要实现的方法

    @optional则是可做或不做。比如TUIableView  delegate 里面的:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    这个就是可要可不要

    关于delegate  protocol 网上有一个例子讲的非常形象:

               我上班的工作主要内容包括 (1)写代码(2)写文档(3)测试程序(4)接电话(5)会见客户

    (1)(2)我自己全权负责,但是后面(3)(4)(5)我不想或者不方便自己做,所以我想找个助手(delegate)帮我做这些事,于是我定了一个招聘要求(Protocol),里写明我的助手需要会做(3)(4)(5)这三件事。很快,我招到一个助手。

            即:我.delegate = 助手;

    于是以后每当我遇到需要测试程序或者接电话的活,我就把他转交给助手(delegate)去处理,助手处理完后如果有处理结果(返回值)助手会告诉我,也许我会拿来用。如果不需要或者没有结果,我就接着做下面的事。。

    下面实现一个简单的protocol 

    在DelegateTest.h里实现protocol 

    //定义protocol 协议
    #import <UIKit/UIKit.h>
    
    @protocol DelegateTestDelegate;
    @interface DelegateTest : NSObject
    {
        id<DelegateTestDelegate> delegate;
    }
    @property(nonatomic,assign)id<DelegateTestDelegate> delegate;
    @property(nonatomic,assign)NSInteger nb;
    -(void)printPublic;         //公开
    @end
    
    //协议里的方法
    @protocol DelegateTestDelegate<NSObject>
    
    -(void)print:(NSInteger)number;        //如果别的类也用到了这个protocol 那么就可以直接调用了。
    -(void)print;
    @end

    DelegateTest.m 文件

    #import "DelegateTest.h"
    
    @interface DelegateTest ()
    
    @end
    
    @implementation DelegateTest
    @synthesize delegate;
    @synthesize nb;
    
    //如果这个类是基于UIControllView的话。可以直接在ViewDidLoad里面调用。那么效果也是一样的  这里就相当于别的类里调用这个方法。起到激活的作用
    -(void)printPublic
    {
        [delegate print];
        [delegate print:nb];
    }
    
    
    @end

    ViewController.h 文件

    #import <UIKit/UIKit.h>
    #import "DelegateTest.h"
    @interface ViewController : UIViewController<DelegateTestDelegate>  //这里的DelegateTestDelegate也就相当于UITableView里的UITableViewDelegate
    {
        DelegateTest *delegateTest;
    }
    
    @property (nonatomic, assign)DelegateTest *delegateTest;
    @end

    ViewController.m 文件

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    @synthesize delegateTest;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        delegateTest = [[DelegateTest alloc]init];
        [delegateTest setDelegate:self];  //设置代理
        [delegateTest printPublic ];  //选择调用delegateTest 里的这个方法。然后就可以调用下面的print了。也就相当于把print给激活了
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    //调用DelegateTest  protocol 里面的方法
    -(void)print
    {
        NSLog(@"qingjoin print succeed");
    }
    -(void)print:(NSInteger)number
    {
        NSLog(@"%d",number);
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    @end

    当调试运行到ViewController.m文件里的ViewDidLoad 时。就会自动调用 print 

    输出:

    2012-11-22 14:09:48.629 DelegateDemo[1875:f803] qingjoin print succeed

    2012-11-22 14:09:48.630 DelegateDemo[1875:f803] 0

    我的理解还很浅显。如果有错误请指正。

        

  • 相关阅读:
    算法导论笔记:21用于不相交集合的数据结构
    算法导论笔记:19斐波那契堆
    xfs管理2T以上大分区
    tcpdump确认服务器连接的交换机信息
    pcp分布式监控工具
    ssh相关命令
    一次ddos攻击
    ssh-agent && ssh-agent forward && SSH ProxyCommand
    变更hostname
    yum第三方源
  • 原文地址:https://www.cnblogs.com/qingjoin/p/2782602.html
Copyright © 2011-2022 走看看