View controller contents when using Interface Builder

I am trying to define a series of container view controllers using Interface Builder (maybe my first mistake). In the storyboard, I created a top view controller, added 3 container views that automatically added each child view controller to the storyboard. I have added an output for each type of container and I can successfully maneuver through child views, hiding / showing container views. There really isn’t much code to say, it’s simple:

-(IBAction) button1Pushed:(id)sender
{
     containerView2.hidden = true;
     containerView1.hidden = false;
}

While this works, I need to update the content every time I show it. viewWillAppear (and related functions) fire for Child View controllers only at initial creation, and not when hiding / showing containers. I suggested that I could add something like:

    [childVC1 updateContent];
    containerView1.hidden = false;

, viewWillAppear ( ). :

  • Apple Programming Guide :
 [self addChildViewController:content];                 // 1
 [self.view addSubview:self.currentClientView];
 [content didMoveToParentViewController:self];          // 3

IB? IB. IB addChildViewController, VC , [self childViewControllers]. viewWillAppear , , IB ?

, 1: Interface Builder, IB ?

  • viewDidLoad:
for ( UIViewController *vc in [self childViewControllers])
{
    [self.view addSubview:vc.view];
    [vc didMoveToParentViewController:self];
}

, . IB , , / .

 vc.view.hidden = true;

. reset , IB .

№ 2: IB, , IB? ? ( [self childViewControllers])? - ?

:

  • IB
  • / , .
  • viewWillAppear , ,

, !

+4
2

1: ( ), segues. , segue , , , , . , segue, . addChildViewController, didMoveToParent ..

: , . , №1 №2. , viewWillAppear, WillAppear, , - VC . , , , ( , ).

( Apple, , ) - updateViewContraints layoutSubviews. ViewDidLayoutSubviews. , , [yourViewController.view setsNeedsLayout]. UIView needsLayout.

, , , viewController, - :

1: ,

2: setsNeedsLayout viewControllers view

3: .

+7

viewWillAppear , . .

IB, segue .

segue , prepareForSegue if/else if/else if segue. segue, :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([segue.identifier isEqualToString: @"firstChildID")
    self.firstChildVC =   (FirstChildVC *) segue.destinationViewController;
  else if ([segue.identifier isEqualToString: @"secondChildID")
    self.secondChildVC =   (SecondChildVC *) segue.destinationViewController;
  else if ([segue.identifier isEqualToString: @"thirdChildID")
    self.thirdChildVC =   (ThirdChildVC *) segue.destinationViewController;
}

, , , :

@protocol ChildVCProtocol

- (void) getReadyToShowView;
//whatever other methods
@end

VC- . getReadyToShowView. , VC .

+2

Source: https://habr.com/ru/post/1546429/


All Articles