Objective-C protocol uses three steps

Source: Internet
Author: User

The Protocol application calls each other between two controllers or between the Controller and the custom uiview.

Its Applications are similar to interfaces in Java or C. Assume that a calls B and B calls back a, so we can design it as follows:

First, define a protocal:

@ Protocol ptlchart


@ Optional
-(Void) callback :( ID) ARG;

@ Required
-(Void) loaddata;
@ End

 

 

Step 2: Define protocal delegate in B:

Code

// B. H
@ Interface B: uiview {
ID <ptlchart> _ delegate;
}

@ Property (nonatomic, retain) ID <ptlchart> delegate;

@ End

// B. M:
# Import "B. H"

@ Implementation B

@ Synthesize delegate = _ delegate;

-(Void) dealloc {
[_ Delegate release];
}
@ End

 

 

Step 3: Pass the delegate when calling B in:

Code

// A. H:
@ Interface A: uiviewcontroller <ptlchart>

@ End
// A. M:
# Import "A. H"

@ Implementation

-(Void) loaddata {
A * A = [[A alloc] init];
[A setdelegate: Self]; // <--------- here
[Self. View addsubview: A];
[A release];
}

-(Void) callback :( ID) Arg {
Int I = (INT) ARG;
Nslog (@ "Callback = % d", I );
}
@ End

 

 

 

 

 

 

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.