I'mm successfully embedding and swopping uiviewcontrollers within a container view. Now I want to send a message from the child uiviewcontrollers up to the parent uivewcontroller. I'm wiring them up as delegates but can't figure out how the assign it as a delegate in the parent view
parent.h -load delegate
// Import child delegates
#import "GenWarnDangerVC.h"
@interface Appliance_IPVC : UIViewController <ChildViewControllerDelegate>
{
}
parent.m -load child view
- (void)viewDidLoad
{
[super viewDidLoad];
// * Add child views
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildFour"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildOne"]]; // <-- this is the delegate
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildTwo"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildThree"]];
self.currentChildController = self.childViewControllers[0];
self.currentChildController.view.frame = self.containerView.bounds;
[self.containerView addSubview:self.currentChildController.view];
for (UIViewController *controller in self.childViewControllers)
[controller didMoveToParentViewController:self];
// Tried making it delegate here, complies but zilch happens
UIStoryboard *storyboard = self.storyboard;
GenWarnDangerVC *_GenWarnDangerVC = [storyboard instantiateViewControllerWithIdentifier:@"ChildOne"];
_GenWarnDangerVC.delegate=self;
}
We swop in later during runtime with
[self transitionFromViewController:oldController toViewController:newController duration:0.33 options:options animations:^{} completion:nil];
childview.h - do delegate setting up stuff
#import <UIKit/UIKit.h>
@protocol ChildViewControllerDelegate;
@interface GenWarnDangerVC : UIViewController <UITextViewDelegate>
@property (nonatomic, weak) id<ChildViewControllerDelegate> delegate;
@end
@protocol ChildViewControllerDelegate <NSObject>
- (void)animateTextField:(BOOL)up;
@end
childview.m - send message to parent
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
{
//if ([self.delegate respondsToSelector:@selector(animateTextField:)])
//{
// Sending delegate message
NSLog(@"Sending delegate message");
[self.delegate animateTextField:YES];
//}
return YES;
}
The parent view never responds, it handles [self animateTextField:YES] when called within the parent view (itself) but never 'hears' from the child.
I'm guessing because we need to tell the child view who it's a delegate for, in the parent view, with something like
UIStoryboard *storyboard = self.storyboard;
GenWarnDangerVC *_GenWarnDangerVC = [storyboard instantiateViewControllerWithIdentifier:@"ChildOne"];
_GenWarnDangerVC.delegate=se
lf;
But (a) what exactly? And (b) is it done when the cild views are loaded? or when they are swapped in?