0

containerView

I don't have a problem displaying each container view when switching between them using the segmented controller. What I do have a problem with is that whenever the parent view loads, both the blue and green container views also load.

I want the blue to load with the parent view controller and the green to load only when I switch the segmented control. Any Ideas?

  • By loading I mean that viewDidLoad, ViewDidAppear, ViewDidlayoutSubviews, etc.. gets called
override fund viewDidload() {
super.viewdidload()

   let child = GreenViewController()
   addChild(child)
   child.view.frame = frame
   view.addSubview(child.view)
   child.didMove(toParent: self)
}

I tried using the code above, but it doesn't seem to make a difference as loading for both child view controllers still load once the parent loads

Chris Comas
  • 216
  • 4
  • 10
  • I am just wondering are you two container views to your main view controller? Because as far as I am aware you can only have one embed segue per container view – Andrew Jun 22 '20 at 19:41
  • yes, they are, I am using 2 different container views just for simplicity and organization. Each container view will have its own table view. I want to transition between them easily using my segmented control. however, I want the blue one to load automatically and the green one to load only when the segmented control is switched to "second" – Chris Comas Jun 22 '20 at 19:54
  • 1
    Because you have both declared in the storyboard then they will both load then the storyboard launches. You should only have one container view. It is probably better to add the view controllers programatically to the container view rather than trying to do this in storyboards,. – Andrew Jun 22 '20 at 19:58
  • 1
    Check you this tutorial by John Sundell that explains how to do it programatically (note he is loading the child view controller on to the main view, but it is easy to switch that to add the child to any view that you want) https://www.swiftbysundell.com/basics/child-view-controllers/ – Andrew Jun 22 '20 at 20:00
  • 1
    Also look at this tutorial by Paul Hudson, it is about the coordinator pattern, however there is a really interesting bit about *Storyboarded* a protocol for loading view controllers from storyboards, that may be useful for you too. https://www.hackingwithswift.com/articles/71/how-to-use-the-coordinator-pattern-in-ios-apps – Andrew Jun 22 '20 at 20:01

1 Answers1

0

As i understand there are 2 container views and you want to see the blue view when the segmented is first and you want to see the green view when the segmented is second if it is true the solution is like this;

You should init the uivew(hold on the ctrl and drag it to viewcontroller for both blue and green container view)(you need to add 2 different container view) and write

if segmented.selectedIndex == 0 { 

    greenView.isHidden = true
    blueView.isHidden = false

} else if segmented.selectedIndex == 1 {

    greenView.isHidden = false
    blueView.isHidden = true
}
Andrew
  • 26,706
  • 9
  • 85
  • 101
Burak Köse
  • 109
  • 4