2013-04-10 66 views
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視圖時完成?或者當它們交換時?

回答

0

的問題,我是如何初始化子控制器

[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildOne"]]; 

打好這加載的控制器,但沒有離開到自身的引用,因此它可以被分配一個委託。

但是這樣做。好極了。

GenWarnDangerVC * GenWarnDanger_vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildOne"]; 
[self addChildViewController:GenWarnDanger_vc]; 
GenWarnDanger_vc.delegate = self; 
相關問題