0
我成功地將uiviewcontrollers嵌入並在容器視圖中切換。現在我想從孩子uiviewcontrollers發送一條消息給父母uivewcontroller。我它們連接起來作爲代表,但無法弄清楚如何將其指定爲父視圖的委託將父級uiviewcontroller作爲代理添加到由addChildViewController添加的uivewcontroller
parent.h -load委託
// Import child delegates
#import "GenWarnDangerVC.h"
@interface Appliance_IPVC : UIViewController <ChildViewControllerDelegate>
{
}
parent.m -load子視圖
- (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中串換中 - 做委託設立塞入˚F
#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;
}
父視圖從不響應,它處理[自animateTextField:YES]時父視圖中調用(自身),但從來沒有「聽到'從孩子。
我猜是因爲我們需要告訴孩子視圖誰它是一個代表,在父視圖,類似
UIStoryboard *storyboard = self.storyboard;
GenWarnDangerVC *_GenWarnDangerVC = [storyboard instantiateViewControllerWithIdentifier:@"ChildOne"];
_GenWarnDangerVC.delegate=se
LF;
但是(a)究竟是什麼? (b)是否在加載cild視圖時完成?或者當它們交換時?