1

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];
JulianB
  • 1,686
  • 1
  • 19
  • 31
  • 1
    "In the parent I want to access the UITextView" Stop wanting that. You should _never_ talk directly to another view controller's outlet. Give the child view controller a _method_ so that you can call that and query the child's data. – matt Mar 04 '20 at 15:56
  • Also your setup code is very weird because you call `addChildViewController` twice; the first time, you add "ChildFour", but you never _do_ anything with it. So it is sitting there, sort of in your way, in your `childViewControllers`. – matt Mar 04 '20 at 15:57
  • @matt snipped code, there's four of them, which swap out dynamically. So... self.currentChildController.methodName ? (they are already delegates I believe) – JulianB Mar 04 '20 at 16:29
  • You could probably say `[self.currentChildController methodName];`, because of dynamic typing. Properly, you'd do better to _cast_ `currentChildController` to the type of view controller you think it is. – matt Mar 04 '20 at 16:41

1 Answers1

1

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";
}
Asperi
  • 228,894
  • 20
  • 464
  • 690