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:
This is my parent view controller:
Click the button and the Child appears:
Write something in the text field and press the button to pass it back:
The data is displayed on a label in the parent view controller.
Here are the files for this:
ParentViewController.h
#import <UIKit/UIKit.h>
#import "ChildViewController.h"
@interface ParentViewController : UIViewController <ChildViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *label;
@end
ParentViewController.m
@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
ChildViewController.h
#import <UIKit/UIKit.h>
@protocol ChildViewControllerDelegate <NSObject>
-(void)passDataBack:(NSString*)data;
@end
@interface ChildViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *txtfield;
@property (nonatomic, weak) id <ChildViewControllerDelegate> delegate;
- (IBAction)passingBack:(id)sender;
@property NSString *data;
@end
ChildViewController.m
#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
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.
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:
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