title: Running into infinite loop with View controller Delegation
body: <p>I have the following code, and have been struggling with it for quite some time. I have two view controllers, FirstView and SecondView. I pushViewController from FirstView to SecondView. In SecondView there is a UITextView where I take user's input. Then I save that input using a delegate in SecondView to FirstView's variable called text. When I run this, the code gets into an infinite loop on calling the delegate from SecondView.</p>
  
  <p>FirstView.m</p>
  
  <pre><code>UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
  SecondView *secondView = [story instantiateViewControllerWithIdentifier:@"SecondView"];
  secondView.delegate = self;
  [self.navigationController pushViewController:secondView animated:YES];
  
  -(void)setText:(NSString *)strData
  {
  NSLog(@"Entered setText delegate");
  NSLog(@"Current string is %@", strData);
  self.text = strData;
  }
  </code></pre>
  
  <p>SecondView.h</p>
  
  <pre><code>@protocol SetInstructionDelegate &lt;NSObject&gt;
  -(void)setText:(NSString *)strData;
  @end
  
  @property (weak, nonatomic) IBOutlet UITextView *textView;
  @property (nonatomic, weak) id&lt;SetInstructionDelegate&gt; delegate;
  </code></pre>
  
  <p>SecondView.m</p>
  
  <pre><code>-(void)viewDidLoad {
  /****************************** Done Button framing ********************************/
  UIButton *btn_bar=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
  [btn_bar setBackgroundColor:[UIColor clearColor]];
  [btn_bar setTitle:@"Done" forState:UIControlStateNormal];
  [btn_bar addTarget:self action:@selector(doneEditing:) forControlEvents:UIControlEventTouchUpInside];
  UIBarButtonItem *doneEdit=[[UIBarButtonItem alloc]initWithCustomView:btn_bar];
  self.navigationItem.rightBarButtonItem=doneEdit;
  }
  
  -(void)doneEditing:(id) sender
  {
  [self.view.window endEditing: YES];
  [self.navigationController popViewControllerAnimated:YES];
  NSLog(@"Current text is : %@", self.textView.text);
  [[self delegate] setText:self.textView.text];
  }
  </code></pre>
  
  <p>The code stays in doneEditing, and keeps printing the NSLogs repeatedly. I read quite a few links on this but cannot find a definite answer and have been struggling. I am very new to iOS and delegate methods. Any help would be appreciated. Thanks!</p>