1

When I add a UIContainerView to a view controller it's type is UIView.

How do I get to the UIViewController for the embedded view?

I need to set some properties of the embedded view view controller.

Thank you

Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
John Cashew
  • 1,078
  • 2
  • 12
  • 28

3 Answers3

2

In prepareForSegue:sender: in the containing view controller, you access the embedded view controller as segue.destinationViewController. If you have multiple segues out of the containing view controller, you'll want to give each one a unique identifier string in the storyboard, so you can check segue.identifier in prepareForSegue:sender: to see which segue you're dealing with.

You can save segue.destinationViewController in an instance variable if you need to send it more messages later, after prepareForSegue:sender: has returned.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
1

If you are using storyboard, you can access the child view controller in the prepareForSegue method, the segue is from the container view to its child view controller, if you're using code to add child view controller, you can access it directly

MudOnTire
  • 506
  • 3
  • 9
  • Thank you for your reply. I am using storyboards, however there is no segue. It is simply a container view that contains an embedded UIViewController. – John Cashew Apr 02 '16 at 04:53
  • Sorry for my dumb reply. You were talking about the embed segue. I want to access the view controller unrelated to that. Should I save the embedded viewcontroller pointer from this segue? – John Cashew Apr 02 '16 at 04:59
  • I think you just need to create an instance of the view controller you want to embed and set the container to be the embedded controller's view – MudOnTire Apr 02 '16 at 05:04
  • Can you past your code so can see how much you have done – MudOnTire Apr 02 '16 at 05:20
  • It's all done in the the IB by linking the storyboards, so in this case there is no code to paste. – John Cashew Apr 02 '16 at 05:55
1

In UIContainerView, you use Storyboard directly Embed to UIViewController, or you use coding way access childViewController, First, i have the storyboard solution is given below,

i am create parentViewController add UIContainerView then drag to ChildViewController show option to select the Embed finally run your project

Then Another Way to Embed the ChildViewController from ParentViewController using Coding format is given below

@property (weak, nonatomic) UIViewController *currentViewController;
@property (weak, nonatomic) IBOutlet UIView *containerView;

ViewDidLoad method:

_currentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"childViewController"];
    _currentViewController.view.layer.cornerRadius = 8.0f;
    _currentViewController.view.translatesAutoresizingMaskIntoConstraints = NO;

    [self addChildViewController:_currentViewController];
    [self addSubview:_currentViewController.view toView:_containerView];



- (void)addSubview:(UIView *)subView toView:(UIView*)parentView {
    [parentView addSubview:subView];

    NSDictionary * views = @{@"subView" : subView,};
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subView]|"
                                                                   options:0
                                                                   metrics:0
                                                                     views:views];
    [parentView addConstraints:constraints];
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subView]|"
                                                          options:0
                                                          metrics:0
                                                            views:views];
    [parentView addConstraints:constraints];
}

hope its helpful..

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30