title: Passing data between 2 UIViewController using delegate and protocol
body: <p>I know this question has been asked and answered many times over here. But I am dealing with this thing for the first time and still not able to get the perfect implementation of it in my mind. Here's the code I have the delegate method I implement to pass data from <code>SecondViewController</code> to <code>FirstViewController</code>.</p>
  
  <p><em><strong>FirstViewController.h</em></strong></p>
  
  <pre><code>#import "SecondViewController.h"
  
  @interface FirstViewController : UITableViewController&lt;sampleDelegate&gt; 
  @end
  </code></pre>
  
  <p><em><strong>FirstViewController.m</em></strong></p>
  
  <pre><code>@interface FirstViewController ()
  
  // Array in which I want to store the data I get back from SecondViewController.
  @property (nonatomic, copy) NSArray *sampleData;
  @end
  
  @implementation FirstViewController
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
     SecondViewController *controller = [[SecondViewController alloc] init];          
     [self.navigationController pushViewController:controller animated:YES];
  }
  @end
  </code></pre>
  
  <p><em><strong>SecondViewController.h</em></strong></p>
  
  <pre><code>@protocol sampleDelegate &lt;NSObject&gt;
  - (NSArray*)sendDataBackToFirstController;
  @end
  
  @interface SecondViewController : UITableViewController
  @property (nonatomic, strong) id &lt;sampleDelegate&gt; sampleDelegateObject;
  @end
  </code></pre>
  
  <p><em><strong>SecondViewController.m</em></strong></p>
  
  <pre><code>@interface SecondViewController ()
  @property (strong, nonatomic) NSArray *dataInSecondViewController;
  @end
  
  @implementation SecondViewController
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      self.dataInSecondViewController = [NSArray arrayWithObjects:@"Object1", @"Object2", nil];
  }
  
  - (NSArray*)sendDataBackToFirstController
  {
      return self.dataInSecondViewController;
  }
  @end
  </code></pre>
  
  <p>Am I doing it correctly? All I want it to send the data in <code>self.dataInSecondViewController</code> to <code>FirstViewController</code> and store it over there in the <code>NSArray</code> property <code>sampleData</code> of <code>FirstViewController</code>.</p>
  
  <p>Somehow I am not able to access <code>sendDataBackToFirstController</code> in <code>FirstViewController</code>. What other things I am missing implementing to access <code>sendDataBackToFirstController</code> there?</p>