title: custom delegate being null iOS
body: <p>I know this should be a simple thing to fix, but I can't see what's going wrong. May be extra pair will help. Here is what I am trying to do.</p>
  
  <ol>
  <li>In my table view controller, there is an (+) button on the navigation controller to add new item.</li>
  <li>I have a modal segue that takes it to the next view controller. User fills in a form and hit saves the table view controller reloads with the newly added record.</li>
  </ol>
  
  <p>To do this, I implemented protocol with a delegate.</p>
  
  <p><strong>MyFormViewController.h</strong></p>
  
  <pre><code>protocol MyCustomDelegate &lt;NSObject&gt;
  
  @required
  
  - (void)addNewRecord:(myFormViewController *)formViewController itemToAdd:(Item *)item;
  
  @end
  
  @property (nonatomic,weak) id&lt;MyCustomDelegate&gt; delegate;
  </code></pre>
  
  <p><strong>MyFormViewController.m</strong></p>
  
  <pre><code>@synthesize delegate;
  
  - (IBAction)addItem:(id)sender {
      Item *item = [[Item alloc]init];
  
      item.name = itemName.text;
      item.desc = itemDescription.text;
  
      // I am having problem here, self.delegate is being null even though, it's being set in    prepareForSegue.
      if ([self.delegate respondsToSelector:@selector(addNewRecord:)]) {
  
          [self.delegate addNewRecord:self itemToAdd:item];
      }
      else{
  
          // delegate is getting set to null for some reason.
          NSLog(@"Delegate method not getting called...%@",delegate);
      }
  }
  </code></pre>
  
  <p>in <strong>MyTableViewController.h</strong></p>
  
  <p>@interface MyTableViewController : UITableViewController </p>
  
  <p><strong>MyTableViewController.m</strong></p>
  
  <pre><code>-(void)addItem:(myFormViewController *)formViewController itemToAdd:(Item *)item{
  
      if(item)
      {
          MyClass *_itemClass = [[MyClass alloc]initWithPath:@"items/"];
  
          [_itemClass addItemForUser:item];
      }
  
      [formViewController dismissViewControllerAnimated:YES completion:nil];
  }
  </code></pre>
  
  <p>in my <strong>prepareForSegue</strong>  method I am setting my tableviewcontroller as delegate.</p>
  
  <pre><code>-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  
     if ([segue.identifier isEqualToString:@"addItemSegue"]){
  
      myFormViewController *_showaddTopic = [[myFormViewController alloc]init];
  
      _showaddTopic.delegate = self;
  }
  </code></pre>
  
  <p>After all this, my delegate in myFormViewController is being set to "null". I am not sure why it's not working. It's pretty basic stuff but giving me hard time.</p>
  
  <p>Thank you</p>