Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This below code just show the very basic use of delegate concept .. you name the variable and class as per your requirement.</p> <p>First you need to declare a protocol:</p> <p>Let's call it <strong>MyFirstControllerDelegate.h</strong> </p> <pre><code>@protocol MyFirstControllerDelegate - (void) FunctionOne: (MyDataOne*) dataOne; - (void) FunctionTwo: (MyDatatwo*) dataTwo; @end </code></pre> <p>Import <strong>MyFirstControllerDelegate.h</strong> file and confirm your <strong>FirstController</strong> with protocol <strong>MyFirstControllerDelegate</strong></p> <pre><code>#import "MyFirstControllerDelegate.h" @interface FirstController : UIViewController&lt;MyFirstControllerDelegate&gt; { } @end </code></pre> <p>In the implementation file, you need to implement both functions of protocol:</p> <pre><code>@implementation FirstController - (void) FunctionOne: (MyDataOne*) dataOne { //Put your finction code here } - (void) FunctionTwo: (MyDatatwo*) dataTwo { //Put your finction code here } //Call below function from your code -(void) CreateSecondController { SecondController *mySecondController = [SecondController alloc] initWithSomeData:.]; //..... push second controller into navigation stack mySecondController.delegate = self ; [mySecondController release]; } @end </code></pre> <p>in your <strong>SecondController</strong>:</p> <pre><code>@interface SecondController:&lt;UIViewController&gt; { id &lt;MyFirstControllerDelegate&gt; delegate; } @property (nonatomic,assign) id &lt;MyFirstControllerDelegate&gt; delegate; @end </code></pre> <p>In the implementation file of <strong>SecondController</strong>.</p> <pre><code>@implementation SecondController @synthesize delegate; //Call below two function on self. -(void) SendOneDataToFirstController { [delegate FunctionOne:myDataOne]; } -(void) SendSecondDataToFirstController { [delegate FunctionTwo:myDataSecond]; } @end </code></pre> <p><a href="http://en.wikipedia.org/wiki/Delegation_pattern#Objective-C_example" rel="noreferrer">Here</a> is the wiki article on delegate. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload