我成功地在容器视图中嵌入和交换 uiviewcontrollers。现在我想从子 uiviewcontroller 向父 uivewcontroller 发送一条消息。我将它们连接为代表,但无法弄清楚如何在父视图中将其分配为代表
parent.h - 加载委托
// Import child delegates
#import "GenWarnDangerVC.h"
@interface Appliance_IPVC : UIViewController <ChildViewControllerDelegate>
{
}
parent.m - 加载子视图
- (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;
}
我们稍后在运行时使用
[self transitionFromViewController:oldController toViewController:newController duration:0.33 options:options animations:^{} completion:nil];
childview.h - 做委托设置的东西
#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 - 向父母发送消息
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
{
//if ([self.delegate respondsToSelector:@selector(animateTextField:)])
//{
// Sending delegate message
NSLog(@"Sending delegate message");
[self.delegate animateTextField:YES];
//}
return YES;
}
父视图从不响应,它在父视图(本身)中调用时处理 [self animateTextField:YES],但从不从子视图中“听到”。
我猜是因为我们需要在父视图中告诉子视图它是谁的代表,例如
UIStoryboard *storyboard = self.storyboard;
GenWarnDangerVC *_GenWarnDangerVC = [storyboard instantiateViewControllerWithIdentifier:@"ChildOne"];
_GenWarnDangerVC.delegate=se
如果;
但是(a)究竟是什么?并且 (b) 是在加载 cild 视图时完成的吗?或者当他们被交换?