user3762780
user3762780

Reputation: 45

SetViewControllers with uiPageViewControllers is not working

I am making an app that uses a UIPageViewController for navigation between pages. Everything works fine except that when I am trying to manually change which view controller is shown using setViewControllers it does not show.

I know that it is creating the view controller because it prints to the log, but the controller shown on screen isn't changed. The indexes I use for the subclass of uiviewcontroller that makes each view controller are also set correctly.

The function is called on the press of a view controller.

Here's the code that is supposed to change the controller:

    func changeVC(VC: UIViewController) {
        setViewControllers([VC], direction: .Forward, animated: true, completion: nil)
    }

And the function call:

    Let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("contentView") as! ViewController
    Controller.index = row
    PageViewController().changeVC(controller)

That is inside of the didSelectRowAtIndexPath method of a table view.

Upvotes: 3

Views: 3834

Answers (1)

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20369

Cjay was absolutely correct,

Lemme explain it little more in detail to help you understand what mistake you are doing.

Everytime you call PageViewController().changeVC(controller) new instance of PageViewController will be created and changeVC will be called on that new instance that you have just created. Though your code to instantiate a ViewController from storyboard setting it's index,calling changeVC and setting theViewController of PageViewController everything executes you won't see any of this making any effect on your screen because you changed the ViewController of the PageViewController which you just instiated which neither has a visible valid frame (0,0,0,0) nor it is loaded (not added to your ViewController's view) :)

Rather what you should have done is to do exact same things on a PageViewController which is already loaded :)

As per you comments you are loading the PageViewController from storyboard create an IBOutlet for it lets call it as myPageViewController :) so when you want to change the ViewController of it simply say,

let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("contentView") as! ViewController
controller.index = row
self.myPageViewController.changeVC(controller)

Hope this helps :)

Upvotes: 1

Related Questions