View doesn't show with "setViewController" in a UIPageViewController in Swift - ios

I would like to change the pages showed by my UIPageView programatically.
I created two ViewController that are intended to be displayed in the PageView, and I set the initial View with "setViewControllers" in my viewDidLoad. I works, the view is displayed and I can swipe on the screen to switch between the two views.
My problem being that when I try to call again "setViewControllers" to change the displayed view when I click a button, nothing happens.
I'm using delegates since the buttons are part of another ViewController, and used NSLog to make sure the functions were called, which is the case.
If anyone could help me understand why the "setViewControllers" method doesn't do anything, that would be great.
Here is my code for my UIPageViewController:
import UIKit
class FrontContentController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, sideMenuDelegate {
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return subViewControllers.count
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
if currentIndex <= 0 {
return nil
}
return subViewControllers[currentIndex - 1]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
if currentIndex >= subViewControllers.count - 1 {
return nil
}
return subViewControllers[currentIndex + 1]
}
lazy var subViewControllers:[UIViewController] = {
return [
UIStoryboard(name: "frontPage", bundle: nil).instantiateViewController(withIdentifier: "FirstContent") as! FirstContentController,
UIStoryboard(name: "frontPage", bundle: nil).instantiateViewController(withIdentifier: "SecondContent") as! SecondContentController
]
}()
var sideMenuVC : SideMenuController!
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
self.sideMenuVC = (self.storyboard?.instantiateViewController(withIdentifier: "SideMenu") as! SideMenuController)
self.setViewControllers([subViewControllers[0]], direction: .forward, animated: true, completion: nil)
// Do any additional setup after loading the view.
}
func changeColor(_ color: UIColor) {
if color == .blue {
self.setViewControllers([subViewControllers[0]], direction: .forward, animated: false, completion: nil)
NSLog("Blue Delegate")
}
if color == .red {
self.setViewControllers([subViewControllers[1]], direction: .forward, animated: false, completion: nil)
NSLog("Red Delegate")
}
}
}
And in case it's relevant, the separate UIViewController with the buttons (a side menu) :
import UIKit
protocol sideMenuDelegate: AnyObject {
func changeColor(_ color : UIColor)
}
class SideMenuController: UIViewController {
weak var delegate : sideMenuDelegate?
var frontPageVC : FrontPageController!
var frontContentPC : FrontContentController!
override func viewDidLoad() {
super.viewDidLoad()
let storyboard = UIStoryboard(name: "frontPage", bundle: nil)
frontPageVC = (storyboard.instantiateViewController(withIdentifier: "FrontPage") as! FrontPageController)
frontContentPC = (storyboard.instantiateViewController(withIdentifier: "FrontContent") as! FrontContentController)
self.delegate = frontContentPC
self.view.backgroundColor = UIColor.black
}
#IBAction func pressBlueButton(_ sender: Any) {
delegate?.changeColor(UIColor .blue)
}
#IBAction func pressRedButton(_ sender: Any) {
delegate?.changeColor(UIColor .red)
}
}
Thanks

Related

How to jump back to a first index of a page in UIPageviewController

I currently have a button that currently displays a UIPageViewController as an overlay on the homescreen. The user can always click on it and it would still display this ViewControllers embedded in a UIPageViewController. However at the moment, when the user clicks on the button to display the overlay and closes it and opens it again. The last page index / state of the last page is saved and displayed again. What i would like to do is to always display the first ViewController in the UIPageviewController. I have tried different ways but haven't had any luck.
Here is the screen capture that shows how the current mechanism works
https://gph.is/g/Z2QlyDa
HomeHelpViewController that contains the UIPageViewController
class HomeHelpViewController: UIPageViewController, UIPageViewControllerDataSource {
var helpPages: [UIViewController] = []
var currentIndex: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
let helpStoryboard = UIStoryboard(name: "HomeHelp", bundle: nil)
for scanIdentifier in ["homeHelpVC1","homeHelpVC2","homeHelpVC3","homeHelpVC4"] {
let scanVC = helpStoryboard.instantiateViewController(withIdentifier: scanIdentifier)
self.helpPages.append(scanVC)
}
self.dataSource = self
self.setViewControllers([self.helpPages.first!], direction: .forward, animated: true)
}
//MARK:- UIPageViewControllerDataSource
func pageViewController(_ pPageViewController: UIPageViewController, viewControllerBefore pViewController: UIViewController) -> UIViewController? {
self.currentIndex = self.helpPages.firstIndex(of: pViewController)!
if self.currentIndex > 0 {
return self.helpPages[self.currentIndex - 1]
}
return nil
}
func pageViewController(_ pPageViewController: UIPageViewController, viewControllerAfter pViewController: UIViewController) -> UIViewController? {
self.currentIndex = self.helpPages.firstIndex(of: pViewController)!
if self.currentIndex < (self.helpPages.count - 1) {
return self.helpPages[self.currentIndex + 1]
}
return nil
}
func presentationCount(for pPageViewController: UIPageViewController) -> Int {
return self.helpPages.count
}
func presentationIndex(for pPageViewController: UIPageViewController) -> Int {
return self.currentIndex
}
}
HomePageViewController that has the button display the UIPageViewController as an overlay
class HomePageViewController: UIViewController {
#IBOutlet weak var helpOverlay: BaseOverlayView!
#IBAction func helpButton(_ pSender: Any) {
self.helpButton.setImage(AppDelegate.helpButtonImage(tutorial: true), for: .normal)
self.helpOverlay.showOnView(self.view)
}
#IBAction func closeHelp(_ pSender: UIButton) {
self.helpOverlay.removeFromView()
self.helpButton.setImage(AppDelegate.helpButtonImage(tutorial: false), for: .normal)
}
}
declare a variable to access your HomeHelpViewController inside your HomePageViewController
class HomePageViewController: UIViewController {
weak var homePageController : HomeHelpViewController?
#IBOutlet weak var helpOverlay: BaseOverlayView!
#IBAction func helpButton(_ pSender: Any) {
if let homePageController = homePageController {
homePageController.setViewControllers([homePageController.helpPages.first!], direction: .forward, animated: true)
}
self.helpButton.setImage(AppDelegate.helpButtonImage(tutorial: true), for: .normal)
self.helpOverlay.showOnView(self.view)
}
#IBAction func closeHelp(_ pSender: UIButton) {
self.helpOverlay.removeFromView()
self.helpButton.setImage(AppDelegate.helpButtonImage(tutorial: false), for: .normal)
}
}
inside this loop assign your HomeHelpViewController
for scanIdentifier in ["homeHelpVC1","homeHelpVC2","homeHelpVC3","homeHelpVC4"] {
let scanVC = helpStoryboard.instantiateViewController(withIdentifier: scanIdentifier) as? HomePageViewController
scanVc?.homePageController = self
self.helpPages.append(scanVC)
}
This may be because the UIPageViewController was not properly dismissed.
func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)
You can refer to the documentation here: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621505-dismiss
This effectively deallocates the UIPageViewController so that once the button is pressed, it will reinitialized again at the first page.

Type 'PageViewController' does not conform to protocol 'UIPageViewControllerDataSource'

I have been trying to use a UIPageViewController in my storyboard to create a set of sliding images, but I keep getting this error:
Type 'PageViewController' does not conform to protocol 'UIPageViewControllerDataSource'
And I don't understand why.
class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
//for scroll view
lazy var subViewControllers:[UIViewController] = {
return[
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Slide_1") as! ViewController_0,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Slide_2") as! ViewController_1,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Slide_3") as! ViewController_2
]
}()
//after viewcontroller
func pageViewController(_pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
if (currentIndex >= subViewControllers.count - 1) {
return nil
}
return subViewControllers[currentIndex + 1]
}
//before viewcontroller
func pageViewController(_pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
if (currentIndex <= 0) {
return nil
}
return subViewControllers[currentIndex - 1]
}
override func viewDidLoad() {
super.viewDidLoad()
//setting the initial view for the slider
setViewControllers([subViewControllers[0]],direction: .forward, animated: true, completion: nil)
}
//making style a normal slide
required init?(coder: NSCoder) {
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Navigation
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return subViewControllers.count
}
}
There should be a space between the _ and the pageViewController parameter in your two methods that are part of the protocol (before/after pages).
The underscore denotes that the method, when called, does not need a label for that parameter.
For example:
func setBlob(_ blob: Blob) -> Bool {
}
Would look like this when it's called:
let myBlob = Blob()
setBlob(myBlob)
If it didn't have the _ there it would expect setBlob(blob: blob). It's convenient to be able to change or ignore the parameter names for the sake of code cleanliness. But since you don't have a space between the parameter label and the actual parameter name, it thinks the parameter name is _pageViewController.
There should be a red icon on the same line where the error shows and you can click that to automatically fill in the missing methods so you can easily see what is missing / incorrect. Your class has to implement all the required methods of the protocol and follow the same method signatures or else you'll get this error.

UIPageViewController setViewControllers

I am trying to implement a PageViewController that switches to a different ViewController when a button is pressed. I can successfully load each "child" ViewController using the setViewControllers method during viewDidLoad().
However when I call the setViewControllers method outside viewDidLoad(), it does not change the current "child" ViewController.
Can you call the method setViewControllers at any point or just once during viewDidLoad()? Also is there a way to change a variable in the pageviewcontrollers dataSource extensions?
My project consists of a entry ViewController with a "Settings" button and a ContainerView. Inside the ContainerView I have the UIPageViewController.
Code for PageViewController:
import UIKit
class PageViewController: UIPageViewController {
var slides = [SlideItem]()
var currentIndex: Int!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
initSlides()
}
override func viewDidLoad() {
super.viewDidLoad()
if let viewController = viewSlideViewController(currentIndex ?? 0){
let viewControllers = [viewController]
setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)
}
dataSource = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewSlideViewController(_ index: Int) -> SlideViewController? {
guard let storyboard = storyboard,
let page = storyboard.instantiateViewController(withIdentifier: "SlideViewController") as? SlideViewController else {
return nil
}
page.slide = slides[index]
page.slideIndex = index
return page
}
func viewSettingsViewController(_ index: Int) -> SettingsViewController? {
guard let storyboard = storyboard,
let page = storyboard.instantiateViewController(withIdentifier: "SettingsViewController") as? SettingsViewController else {
return nil
}
return page
}
func initSlides(){
slides.append(SlideItem.init(filename: "Slide 1", comparison: false, highYield: false, videoURL: nil,
groupOrganSystem: ["A"],
groupMedicalSpecialty: ["B"]))
slides.append(SlideItem.init(filename: "Slide 2", comparison: false, highYield: false, videoURL: nil,
groupOrganSystem: ["A"],
groupMedicalSpecialty: ["B"]))
}
func sidebarCommands(button: String, state: Bool){
switch button {
case "settings":
self.setViewControllers([viewSettingsViewController(0)!], direction: .forward, animated: false, completion: nil)
print("settings")
default:
return
}
}
}
Extensions:
extension PageViewController : UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let viewController = viewController as? SlideViewController,
let index = viewController.slideIndex,
index > 0 {
return viewSlideViewController(index - 1)
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let viewController = viewController as? SlideViewController,
let index = viewController.slideIndex,
(index + 1) < slides.count {
return viewSlideViewController(index + 1)
}
return nil
}
}
You can use UIPageviewController in this way: (inheritance)
https://medium.com/how-to-swift/how-to-create-a-uipageviewcontroller-a948047fb6af
You can call setViewControllers at any point (I've been doing that).
The fact that it does not produce expected results is probably a result of bug in your code, include your code if you want to help out with that.
Thank you for your help, I figured out what I was doing wrong. I didn't have the right reference for the active PageViewController in the ContainerView.
So of course called the setViewControllers function on random PageViewController did not produce the correct results.
I found this answer explains how to correctly get a reference from a ContainerView

Remove Black Edges from UIPageViewController in Swift 3

I am using Swift 3.0 and have created a UIPageViewController currently with two pages (each one is a UIViewController). When I run the app, everything works fine, except that there are black spaces at the bottom and the right side of the app when the ViewControllers are shown. I also cannot see the page dots in the PageViewController. I have implemented the functions to place the dots on the screen:
func pageViewController(pageViewController: PageViewController,
didUpdatePageCount count: Int)
func pageViewController(pageViewController: PageViewController,
didUpdatePageIndex index: Int)
Layout
View Controller Inside the PageViewController
PageViewController Settings
Would anyone know how to fix the black borders around the pages and add the page dots?
Update
import UIKit
class PageViewController: UIPageViewController {
weak var pageDelegate: PageViewControllerDelegate?
private(set) lazy var orderedViewControllers: [UIViewController] = {
// The view controllers will be shown in this order
return [self.newViewController(name: "view1"),
self.newViewController(name: "view2"),
self.newViewController(name: "view3"),
self.newViewController(name: "view4"),
self.newViewController(name: "view5")]
}()
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
delegate = self
if let initialViewController = orderedViewControllers.first {
scrollToViewController(viewController: initialViewController, direction: UIPageViewControllerNavigationDirection.forward)
}
pageDelegate?.pageViewController(pageViewController: self, didUpdatePageCount: orderedViewControllers.count)
}
/**
Scrolls to the next view controller.
*/
func scrollToNextViewController() {
if let visibleViewController = viewControllers?.first,
let nextViewController = pageViewController(self, viewControllerAfter: visibleViewController) {
scrollToViewController(viewController: nextViewController, direction: UIPageViewControllerNavigationDirection.forward)
}
}
/**
Scrolls to the view controller at the given index. Automatically calculates
the direction.
- parameter newIndex: the new index to scroll to
*/
func scrollToViewController(index newIndex: Int) {
if let firstViewController = viewControllers?.first,
let currentIndex = orderedViewControllers.index(of: firstViewController) {
let direction: UIPageViewControllerNavigationDirection = newIndex >= currentIndex ? .forward : .reverse
let nextViewController = orderedViewControllers[newIndex]
scrollToViewController(viewController: nextViewController, direction: direction)
}
}
private func newViewController(name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewController(withIdentifier: "\(name)ViewController")
}
/**
Scrolls to the given 'viewController' page.
- parameter viewController: the view controller to show.
*/
private func scrollToViewController(viewController: UIViewController,
direction: UIPageViewControllerNavigationDirection = .forward) {
setViewControllers([viewController],
direction: direction,
animated: true,
completion: { (finished) -> Void in
// Setting the view controller programmatically does not fire
// any delegate methods, so we have to manually notify the
// 'pageDelegate' of the new index.
self.notifyPageDelegateOfNewIndex()
})
}
/**
Notifies '_pageDelegate' that the current page index was updated.
*/
func notifyPageDelegateOfNewIndex() {
if let firstViewController = viewControllers?.first,
let index = orderedViewControllers.index(of: firstViewController) {
self.pageDelegate?.pageViewController(pageViewController: self, didUpdatePageIndex: index)
}
}
}
// MARK: UIPageViewControllerDataSource
extension PageViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
// User is on the first view controller and swiped left to loop to
// the last view controller.
guard previousIndex >= 0 else {
return orderedViewControllers.last
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
// User is on the last view controller and swiped right to loop to
// the first view controller.
guard orderedViewControllersCount != nextIndex else {
return orderedViewControllers.first
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
}
extension PageViewController: UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers: [UIViewController],
transitionCompleted completed: Bool) {
notifyPageDelegateOfNewIndex()
}
}
protocol PageViewControllerDelegate: class {
/**
Called when the number of pages is updated.
- parameter pageViewController: the PageViewController instance
- parameter count: the total number of pages.
*/
func pageViewController(pageViewController: PageViewController,
didUpdatePageCount count: Int)
/**
Called when the current index is updated.
- parameter pageViewController: the PageViewController instance
- parameter index: the index of the currently visible page.
*/
func pageViewController(pageViewController: PageViewController,
didUpdatePageIndex index: Int)
}
I had the same issue now, I've just solved that problem by changing the background color to white in the viewDidLoad() function
self.view.backgroundColor = .white

iOS Swift - Change UIPageViewController page using UISegmentedControl

I finnaly synchronized my segmentedController to the UIPageViewController in this way: when I swipe between pages, the segmented controller changes it's segment index. I want to know how to do the reverse too, when segmentedController's segment is tapped, to change the pageViewController page by segmentedController index. Here is my code:
class photoPageViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var segmentedControl: UISegmentedControl!
private var pageViewController: UIPageViewController?
var controllers = [thePageViewController]()
var thePage = thePageViewController()
override func viewDidLoad() {
super.viewDidLoad()
createPageViewController()
setupPageControl()
}
private func createPageViewController() {
var pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController
pageController.dataSource = self
pageController.delegate = self
var firstController = getItemController(thePage.itemIndex)!
var startingViewControllers: NSArray = [firstController]
pageController.setViewControllers(startingViewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
pageViewController = pageController
addChildViewController(pageViewController!)
self.view.addSubview(pageViewController!.view)
pageViewController!.didMoveToParentViewController(self)
}
private func setupPageControl() {
let appearance = UIPageControl.appearance()
appearance.pageIndicatorTintColor = UIColor.grayColor()
appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
appearance.backgroundColor = UIColor.darkGrayColor()
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let itemController = viewController as! thePageViewController
if itemController.itemIndex > 0 {
return getItemController(itemController.itemIndex-1)
}
return nil
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let itemController = viewController as! thePageViewController
if itemController.itemIndex+1 < 3 {
return getItemController(itemController.itemIndex+1)
}
return nil
}
func getItemController(itemIndex: Int) -> UIViewController? {
var vc: thePageViewController? = nil
switch itemIndex {
case 0:
vc = self.storyboard!.instantiateViewControllerWithIdentifier("ViewController0") as! firstPhotoViewController
vc?.itemIndex = itemIndex
case 1:
vc = self.storyboard!.instantiateViewControllerWithIdentifier("ViewController1") as! secondPhotoViewController
vc?.itemIndex = itemIndex
case 2:
vc = self.storyboard!.instantiateViewControllerWithIdentifier("ViewController2") as! thirdPhotoViewController
vc?.itemIndex = itemIndex
default:
return nil
}
return vc
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return 3
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
func pageViewController(photoPageViewController: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers pageViewController: [AnyObject],
transitionCompleted completed: Bool)
{
if (!completed)
{
return;
}
segmentedControl.selectedSegmentIndex = thePageIndex
//thePageIndex is a global variable that changes when views from pageViewController appear
}
#IBAction func segmentedFunction(sender: AnyObject) {
switch segmentedControl.selectedSegmentIndex
{
case 0:
getItemController(0)
case 1:
getItemController(1)
case 2:
getItemController(2)
default:
println("...")
}
}}
I think you need to change your code a little bit! In my case I create several vc and put them to one array.
func createArrayOfControllers(){
newsLenta = self.storyboard?.instantiateViewControllerWithIdentifier("NewsLentaTableViewController") as NewsLentaTableViewController
mainPage = self.storyboard?.instantiateViewControllerWithIdentifier("MainPageTableViewController") as MainPageTableViewController
onlinetranslation = self.storyboard?.instantiateViewControllerWithIdentifier("SingleTopicTableViewController") as SingleTopicTableViewController
tableViewControllers = [newsLenta, mainPage, onlinetranslation]
}
where tableViewController is global var var tableViewControllers = [UITableViewController]() ! I signed type TableViewController because all my vs's are type of table View controller. So the next function you need to change is
func viewControllerAtIndex(index : Int) -> UIViewController? {
if index > 2 || index < 0 {
return nil
}
return tableViewControllers[index]
}
In the end you just need to create action of segmentControl and reset your vc by index. Using this function:
func resetToMainPage(index: Int!) {
/* Getting the page View controller */
mainPageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("MainPageViewController") as UIPageViewController
self.mainPageViewController.dataSource = self
self.mainPageViewController.delegate = self
let pageContentViewController = self.viewControllerAtIndex(index)
segmentedControl.selectedSegmentIndex = index
self.mainPageViewController.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
self.mainPageViewController.view.frame = CGRectMake(0, 95, self.view.frame.width, self.view.frame.height)
self.addChildViewController(mainPageViewController)
self.view.addSubview(mainPageViewController.view)
self.mainPageViewController.didMoveToParentViewController(self)
}
My question for your how do you update thePageIndex... I dont get that...
I found the way how to change UIPageViewController's content viewcontroller. I dont know is it right way or wrong but it works. Here is a code:
put this to viewdidload:
segmentControl.addTarget(self, action: "selectPageIndexBySegmentControl", forControlEvents: UIControlEvents.ValueChanged)
put it separte from viewdieload:
func selectPageIndexBySegmentControl(){
switch segmentControl.selectedIndex {
case 0:
println("zero index were selected")
pageContentViewController = self.viewControllerAtIndex(0)
mainPageViewController.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
case 1:
println("first element were selected")
pageContentViewController = self.viewControllerAtIndex(1)
mainPageViewController.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
case 2:
println("second element were selected")
pageContentViewController = self.viewControllerAtIndex(2)
mainPageViewController.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
default:
break
}
}

Resources