title: Passing data and delegation
body: <p>I have got a question about delegation. I am fairly new to programming and I have just managed my first exercise-project in which I use delegation. All it is, is a Parent View Controller that calls a Child View Controller which has a text field and a button in it. I write something in that text field, press the button, the Child VC is dismissed and it passes the text back. It then gets displayed in a label on the Parent. I have got a couple of animations for the transition. It all works. Here are some pictures first and the code:</p>
  
  <p>This is my parent view controller:</p>
  
  <p><a href="https://i.stack.imgur.com/soj4Z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/soj4Z.png" alt="enter image description here"></a></p>
  
  <p>Click the button and the Child appears:</p>
  
  <p><a href="https://i.stack.imgur.com/zZYN7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zZYN7.png" alt="enter image description here"></a></p>
  
  <p>Write something in the text field and press the button to pass it back:</p>
  
  <p><a href="https://i.stack.imgur.com/xc1J5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xc1J5.png" alt="enter image description here"></a></p>
  
  <p>The data is displayed on a label in the parent view controller.</p>
  
  <p>Here are the files for this:</p>
  
  <p>ParentViewController.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "ChildViewController.h"
  
  @interface ParentViewController : UIViewController &lt;ChildViewControllerDelegate&gt;
  
  @property (strong, nonatomic) IBOutlet UILabel *label;
  
  @end
  </code></pre>
  
  <p>ParentViewController.m</p>
  
  <pre><code>@implementation ParentViewController
  
  - (void)viewDidLoad {
      [super viewDidLoad];
  
  }
  - (IBAction)Pressed:(id)sender {
      ChildViewController *childViewController = [[ChildViewController alloc]init];
      [self displayContentController:childViewController];
      childViewController.delegate = self;
  
  }
  
  - (void) displayContentController: (UIViewController*) content {
      [self addChildViewController:content];
      content.view.frame = CGRectMake(0, 115, 320, 240);
      content.view.backgroundColor = [UIColor redColor];
  
  
      CATransition *transition = [CATransition animation];
      transition.duration = 1;
      transition.type = kCATransitionPush;
      transition.subtype = kCATransitionFromLeft;
      [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
      [content.view.layer addAnimation:transition forKey:nil];
      [self.view addSubview:content.view];
      [content didMoveToParentViewController:self];
  
  }
  
  - (void)didReceiveMemoryWarning {
      [super didReceiveMemoryWarning];
  
  }
  
  -(void) passDataBack:(NSString *)data{
  
      self.label.text = data;
  
  }
  
  @end
  </code></pre>
  
  <p>ChildViewController.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @protocol ChildViewControllerDelegate &lt;NSObject&gt;
  
  -(void)passDataBack:(NSString*)data;
  
  @end
  
  @interface ChildViewController : UIViewController
  @property (strong, nonatomic) IBOutlet UITextField *txtfield;
  
  @property (nonatomic, weak) id &lt;ChildViewControllerDelegate&gt; delegate;
  - (IBAction)passingBack:(id)sender;
  
  @property NSString *data;
  
  @end
  </code></pre>
  
  <p>ChildViewController.m</p>
  
  <pre><code>#import "ChildViewController.h"
  
  @interface ChildViewController ()
  
  @end
  
  @implementation ChildViewController
  
  - (void)viewDidLoad {
      [super viewDidLoad];
  
  }
  
  - (void)didReceiveMemoryWarning {
      [super didReceiveMemoryWarning];
  
  }
  
  
  - (IBAction)passingBack:(id)sender {
  
      NSString *itemToPassBack = self.txtfield.text;
  
      [self.delegate passDataBack:itemToPassBack];
  
      [self.childViewControllers lastObject];
  
  
      [self willMoveToParentViewController:nil];
  
      [UIView animateWithDuration:1
                            delay:0.0
           usingSpringWithDamping:1
            initialSpringVelocity:1
                          options:UIViewAnimationOptionCurveEaseIn
                       animations:^{
  
                           self.view.frame = CGRectMake(-320, 115, 320, 240);
  
                       } completion:^(BOOL complete){
  
                               [self.view removeFromSuperview];
                               [self removeFromParentViewController];
                       }];
  
  }
  
  @end
  </code></pre>
  
  <p>Regarding the code, there are a couple of things I am not sure about, as I am learning I copied a lot of code etc... so for example I am not sure as to how to animate the child controller back using the same method as I used to animate it in, or if it matters and what is best practice etc... also, I get a strange message in the console: "Unknown class ViewController in Interface Builder file." but it works, so I am not sure why it's there. For the record, I haven't used the storyboard, only xibs.</p>
  
  <p>My question though is the following: I would like to have the button to dismiss the Child View Controller not in the Child View Controller but in the parent, like so: </p>
  
  <p><a href="https://i.stack.imgur.com/CuxPq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CuxPq.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/fbRTO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fbRTO.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/l9imc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l9imc.png" alt="enter image description here"></a></p>
  
  <p>And that's where I am lost. I just don't know how to trigger the method to pass data back from the Parent View Controller. Can anyone help? many thanks</p>