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