zoukankan      html  css  js  c++  java
  • iOS学习笔记—ObjectiveC 委托、非正式协议、正式协议

     

    一、概念:
    1、委托(delegate,也叫代理):当一个对象接受到某个事件或者通知的时候,会向它的Delegate对象查询它是否能够响应这个事件或者通知,如果可以,这个对象就会给它的Delegate对象发送一个消息(执行一个方法调用)。在这种机制下,您可以不进行子类化和方法重载,而是将自己的定制代码放到委托对象中,从而避免对复杂对象进行修改。当您感兴趣的事件发生时,复杂对象会将消息发送给您定制的委托对象。您可以通过这种“挂钩”执行自己的定制代码,实现需要的行为。
    2、非正式协议(informal protocol):使用类别category来实现,非正式协议是NSObject的一个类别,这样任何类的对象都可以作为委托对象来使用,它可以列出对象能够执行的所有方法,这样用来实现委托, 我们可以使用选择器来判断该非正式协议中是否有这个方法。
    3、正式协议(formal protocol):是一个命名的方法列表,与非正式协议相比不同的是,它要求显示的采用协议,采用协议的方法是在类的@interface声明中列出协议的名称,此时,实现协议的类应该遵守协议,承诺实现协议中的所有方法,否则编译器将会发出警告。
    正式协议协议类似于C++的纯虚函数,协议只有声明,没有实现,用来在子类中实现,协议中的方法有两类属性,@required(默认)和@optional两种,@required属性的要求实现协议的类必须要实现这种方法,而@optional属性的方法则不要求,如果不确定协议是否被实现,可以使用respondsToSelector:@select()来判断。

    二、应用举例

    源码如下: 

    /*********简单的委托举例************/

    @interface A :NSObject

    -(void)Log;

    @end

    @implementation A

    -(void)Log{

        NSLog(@"This is A Log");

    }

    @end

     

    @interface B :NSObject{

       A* delegate;

    }

    @property (nonatomic,retain)A* delegate;

    -(void)callLog;

    @end

    @implementation B

    @synthesize delegate;

    -(id)init{

       if (self = [superinit] ) {

            delegate = [[Aalloc]init];

        }

        returnself;

    }

    -(void)callLog{

        

        NSLog(@"This is B callLog");

        [self.delegateLog];

    }

    @end

    /*********简单的委托举例************/

     

    /*********简单的正式协议举例************/

    //协议--begain

    @protocol myProtocol

    //可选实现的

    @optional

    -(void)optional_print;

    //要求实现的

    @required

    -(void)required_print;

    @end

    //协议--end

     

    //使用协议的类--begain

    @interface  myClass :NSObject<myProtocol>

    -(void)print;

    @end

    @implementation myClass

    -(void)print{

        NSLog(@"This is myClass print");

    }

    //实现可选实现的协议

    -(void)optional_print{

        NSLog(@"This is protocol optional_print");

    }

    //实现必须实现的协议

    -(void)required_print{

        NSLog(@"This is protocol required_print");

    }

    @end

    //使用协议的类--end

    /*********简单的正式协议举例************/

     

     

    /*********简单的非正式协议举例************/

    @interface NSObject(myCategory)

    -(void)informal_protocol_print;

    @end

    @implementation NSObject(myCategory)

    -(void)informal_protocol_print{

        NSLog(@"This is informal_protocol_print");

    }

    @end

    /*********简单的非正式协议举例************/

     

     

    /*********正式协议实现委托举例************/

    //协议--begain

    @protocol myProtocolEx

    //可选实现的

    @optional

    -(void)optional_print;

    //要求实现的

    @required

    -(void)required_print;

    @end

    //协议--end

     

    @interface myClassEx :NSObject{

       id<myProtocolEx> delegate;

    }

    @property (nonatomic,assign)id<myProtocolEx> delegate;

    -(void)print;

    @end

    @implementation myClassEx

    @synthesize delegate;

    -(void)print{

        NSLog(@"This is myClassEx print");

        [self.delegateoptional_print];

        [self.delegaterequired_print];

    }

    @end

     

    @interface myCall :NSObject<myProtocol>{

       myClassEx *cls;

    }

    @property (nonatomic,retain)myClassEx *cls;

    -(void)callPrint;

    @end

    @implementation myCall

    @synthesize cls;

    -(id)init{

       if (self=[superinit]) {

            myClassEx* c = [[myClassExalloc]init];

           self.cls = c;

           cls.delegate = (id)self;

            [crelease];

        }

        returnself;

    }

    -(void)callPrint{

        NSLog(@"This is myCall callPrint");

        [self.clsprint];

    }

    -(void)optional_print{

        NSLog(@"This is myCall implemented formal protocol optional_print");

    }

    -(void)required_print{

        NSLog(@"This is myCall implemented formal protocol required_print");

    }

     

    @end

    /*********正式协议实现委托举例************/

     

    int main(int argc,char *argv[])

    {

        @autoreleasepool {

            

           //委托的使用

           B* b = [[B alloc]init];

            [bcallLog];

            

            [b informal_protocol_print];//非正式协议的使用

            NSLog(@"---------------------------------");

            

           //协议的使用

            myClass *cls = [[myClassalloc]init];

            [clsprint];

            [clsoptional_print];

            [clsrequired_print];

            

            [cls informal_protocol_print];//非正式协议的使用

            NSLog(@"---------------------------------");

            

           //正式协议实现委托的使用

           myCall *call = [[myCallalloc]init];

            [callcallPrint];

            NSLog(@"---------------------------------");

            

           return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

        }

    }

  • 相关阅读:
    BZOJ 2034 【2009国家集训队】 最大收益
    vijos P1780 【NOIP2012】 开车旅行
    BZOJ 2115 【WC2011】 Xor
    BZOJ 3631 【JLOI2014】 松鼠的新家
    BZOJ 4717 改装
    BZOJ 2957 楼房重建
    BZOJ 4034 【HAOI2015】 T2
    BZOJ 1834 【ZJOI2010】 network 网络扩容
    BZOJ 2440 【中山市选2011】 完全平方数
    BZOJ 2733 【HNOI2012】 永无乡
  • 原文地址:https://www.cnblogs.com/watchdatalearn2012620/p/3117772.html
Copyright © 2011-2022 走看看