0

I have one UIViewController with two containers. Each of those containers contains one UIViewController. On the parent view controller I have the segmented control that lets user decide which controller should be currently visible:

@IBOutlet weak var firstView: UIView!
@IBOutlet weak var secondView: UIView!
@IBAction func indexChanged(sender: UISegmentedControl) {
    switch segmentedControl.selectedSegmentIndex {
    case 0:
        firstView.hidden = true
        secondView.hidden = false
    case 1:
        firstView.hidden = false
        secondView.hidden = true
    default:
        break;
    }

}

Now, in each of the two embedded UIViewControllers I have a viewDidLoad function:

override func viewDidLoad(){
    print("first container")

and:

override func viewDidLoad(){
    print("second container")

and my problem is: when user enters the parent view controller he immediately sees in the console:

first container
second container

even though only the first container is visible and the second one is not.

I would like to distinguish which container is currently visible, so that when user enters the parent view controller and sees the first container, he should see only first container in the console. And after hitting the segmented control he should see second container, and so forth.

I tried to add those prints in viewWillAppear but they both load at the same time. How should I modify my code to achieve such effect?

user3766930
  • 5,629
  • 10
  • 51
  • 104
  • Have you tried with `viewDidAppear()`? View are loaded before they are shown, and it's default behaviour that your segmented controller loads them at startup. –  Mar 31 '16 at 15:41
  • Hey, thanks, I just tried with `viewDidAppear` but the behavior is the same - it loads both at once and then nothing shows up when I switch between containers... – user3766930 Mar 31 '16 at 15:47
  • Maybe you can check on this side: http://stackoverflow.com/a/2777460/1585121 –  Mar 31 '16 at 15:49
  • `hidden` views are `appeared` when loaded? – zc246 Mar 31 '16 at 15:49
  • @zcui93 yes, in this case apparently it's a real thing :( – user3766930 Mar 31 '16 at 15:53
  • @Mayerz hm it looks promising, but I don't know in which place and on which variables (as in my example) could I use it... could you give me some example? – user3766930 Mar 31 '16 at 15:56

0 Answers0