title: Using delegate between two viewcontrollers in iphone
body: <p>I have two view controllers: view controller and viewcontroller2nd. I have UILabel in one of them and would like to change it when the button( named Go) in the viewcontroller2nd is clicked. I am using delegates and protocols to do it.</p>
  
  <p>The code looks like this:</p>
  
  <p>ViewController.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "ViewController2nd.h"
  
  @interface ViewController : UIViewController &lt;SecondViewControllerDelegate&gt;
  {
      IBOutlet UILabel *lbl;
      ViewController2nd *secondview;
  
  }
  -(IBAction)passdata:(id)sender;
  
  @end
  </code></pre>
  
  <p>ViewController.m</p>
  
  <pre><code>#import "ViewController.h"
  #import "ViewController2nd.h"
  
  @interface ViewController ()
  
  @end
  
  @implementation ViewController
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  -(void) changeLabel:(NSString*)str{
      lbl.text = str;
  }
  
  -(IBAction)passdata:(id)sender{
      ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
      [self presentViewController:second animated:YES completion:NULL];
  }
  
  @end
  </code></pre>
  
  <p>Viewcontroller2nd.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  
  @protocol SecondViewControllerDelegate &lt;NSObject&gt;
  @optional
  -(void) changeLabel:(NSString*)str;
  @end
  
  @interface ViewController2nd : UIViewController{
  
      IBOutlet UIButton *bttn;
      id &lt;SecondViewControllerDelegate&gt; delegate;
  
  }
  
  @property (retain) id delegate;
  
  -(IBAction)bttnclicked;
  -(IBAction)back:(id)sender;
  @end
  </code></pre>
  
  <p>ViewController2nd.m</p>
  
  <pre><code>#import "ViewController2nd.h"
  #import "ViewController.h"
  
  @interface ViewController2nd ()
  
  @end
  
  @implementation ViewController2nd
  
  @synthesize delegate;
  
  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          // Custom initialization
      }
      return self;
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      // Do any additional setup after loading the view from its nib.
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  -(IBAction)bttnclicked{
     [[self delegate] changeLabel:@"Hello"];
  }
  
  -(IBAction)back:(id)sender{
      [self dismissViewControllerAnimated:YES completion:NULL];
  }
  
  @end
  </code></pre>
  
  <p>The passing of the control between the two views is working correctly. However, when i click the go button in the viewcontroller2nd, it doesn't change the value of the label to Hello. What is wrong with the code? Need some guidance.</p>