Nathannn
Nathannn

Reputation: 161

Connecting UISegmentedControl with UIPageViewController

enter image description here

I want to implement a UISegmentedControl to my UIPageViewController but i cannot add it, i have to add it on my container view. The issue is that it cannot find the two ViewController´s and crashes when i press on the Segmented control.

Can i add a Segmented control programmatically since i cannot drag and drop it on UIPageViewController. I also want to be able to switch views using the Segmented control.

Swift language

Upvotes: 1

Views: 2413

Answers (1)

André Slotta
André Slotta

Reputation: 14040

you have to add the segmentedcontrol to the view that contains the containerview. you do not setup any segues from the segmentedcontrol but do it in code:

@IBAction func segmentedControlValueChanged(sender: UISegmentedControl) {
  if sender.selectedSegmentIndex == 0 {
    let vc = storyboard?.instantiateViewControllerWithIdentifier("RedViewController")
    pageViewController.setViewControllers([vc!], direction: .Reverse, animated: true, completion: nil)
  } else {
    let vc = storyboard?.instantiateViewControllerWithIdentifier("BlueViewController")
    pageViewController.setViewControllers([vc!], direction: .Forward, animated: true, completion: nil)
  }
}

take a look at my demo project: https://www.dropbox.com/sh/sa92iwqmb72dyih/AAByJaLJ_Wk24FMwQcWazPcHa?dl=0

Upvotes: 2

Related Questions