objective-cxcodechildviewcontroller

Cant access uitextfield in childviewcontroller


Childview

@interface GenWarnDangerVC : UIViewController <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *riddorText;

In the parent I want to access the UITextView but can't quite getv the syntax

NSString* tempString = self.currentChildController.view.riddorText;
NSString* tempString = self.currentChildController.riddorText;
NSString* tempString = self.childViewControllers[0].riddorText;
etc

Set up goes like this

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

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

self.currentChildController = self.childViewControllers[0];


self.currentChildController.view.frame = self.containerView.bounds;
[self.containerView addSubview:self.currentChildController.view];

Solution

  • If take into account possible extensibility and/or reusability, here is the safe way to access up-casted specific child controller

    if ([self.currentChildController isKindOfClass:GenWarnDangerVC.class]) 
    {
        GenWarnDangerVC *controller = (GenWarnDangerVC *)self.currentChildController;
    
        // << do anything with "controller" directly access properties & methods 
    
        // for example
        controller.riddorText.text = @"New text";
    }