Objective-c Agreement Lan Yi Education

Source: Internet
Author: User

1, the protocol uses the situation: A class has an instance method, this instance method to the object passed over, restricts it must implement a specific method, and the passed object type is not limited.

2. Format

Protocol name: @protocol protocol Name Protocol name general: class name +delegate.

Protocol content: A specific method

Specific examples:

The instance method in the AA class has a protocol limit.

The BB class is a generic class.

Ii

In Aa.h :

Protocol wording

@protocol aadelegate < NSObject >

Agreement content

-(void) play;


@end


@interface Aa: NSObject

The object of the infection complies with the agreement

-(void) Playwithobject: (ID<AaDelegate>) anyobject;


@end


An example method of AA.M with protocol is written in the paper.


-(void) Playwithobject: ( ID <aa Delegate >) anyobject{

[anyobject play];

}

When introducing the AA Bb class in the Viewcontroller.

Create objects individually. AA class object, invoking an instance method to pass the BB class object into the Times warning: The BB class object must be compliant with the protocol of the instance method in the AA class.

Need to Bb.hin the Bb class:

The class in which the Protocol is introduced

#import "Aa.h"

Let the BB class comply with the agreement:

@interface Bb: NSObject <aaDelegate>

Implement the Protocol content in Bb.h.


This article is from the "Get" blog, so be sure to keep this source http://3517465.blog.51cto.com/3507465/1691280

Objective-c Agreement Lan Yi Education

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.