title: Delegates in iOS
body: <p>I am a newbie to iOS world, so please ignore the obvious.</p>
  
  <p>I am pushing a viewController(HelpViewController) on top of another viewController(MainViewController). When a particular action happens in the HelpViewController, I would like to update a variable inside the MainViewController. I understand for this I need to use delegate.
  Here is my delegate header...</p>
  
  <pre><code>@protocol ViewControllerDelegate &lt;NSObject&gt;
  @required
  - (void) switchToggled:(BOOL)status;
  @end
  // Protocol Definition ends here
  @interface ViewDelegate : NSObject
  {
      // Delegate to respond back
      id &lt;ViewControllerDelegate&gt; _delegate;
  }
  @property (nonatomic,strong) id delegate;
  
  -(void)sendMessage:(BOOL)status; // Instance method
  
  @end
  </code></pre>
  
  <p>and implementation...</p>
  
  <pre><code>@implementation ViewDelegate
  
  @synthesize delegate;
  
  -(id)init {
  
      self = [super init];
  
      return self;
  
  }
  
  -(void)sendMessage:(BOOL)status
  {
      [delegate switchToggled:status];
  }
  
  - (void)dealloc
  {
      [super dealloc];
  }
  @end
  </code></pre>
  
  <p>So Now If I want to implement Protocol ViewControllerDelegate I need to specify in MainViewController, which I do as follows --</p>
  
  <pre><code>MainViewController &lt;ViewControllerDelegate&gt;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>#pragma mark - ViewControllerDelegate delegate
  -(void)switchToggled:(BOOL)status{
      NSLog(@"Switch Toggled(%d) Message passed to MainViewController",status);
  }
  </code></pre>
  
  <p><strong>My question</strong> is how do I specify Object, which delegate property needs to point to, so that it can come back to MainViewController's "switchToggled".</p>
  
  <p>One way I do is by having property inside HelpViewController as follows -</p>
  
  <pre><code>MainViewController.m
  HelpViewController *helpVC = [[HelpViewController alloc] init];
  helpVC.mainView = self;
  [self.navigationController pushViewController:helpVC animated:YES];
  [helpVC release];
  
  HelpViewController.h
      @property (nonatomic) MainViewController *mainView;
  HelpViewController.m
  @synthesize mainView;
  ViewDelegate *myDelegate = [[ViewDelegate alloc] init];
  // assign delegate
  myDelegate.delegate = mainView;
  [myDelegate sendMessage];
  [myDelegate release];
  </code></pre>
  
  <p><strong>Is this correct</strong> way to implement or there is better way to achieve this or am I totally wrong.</p>
  
  <p>Thanks</p>