Maksim Tonyushkin
Maksim Tonyushkin

Reputation: 95

Switch View Controllers in a Page View Controller Programmatically | Swift

I have a UIButton (also created programmatically) in my View Controller that's in the Page View Controller. I would like to switch to another View Controller in that same Page View Controller. Here is what I have tried to use:

@objc func myOpinionAction(_ sender:UIButton!)
{
    PageViewController().setViewControllers([suggestionPage()], direction: UIPageViewController.NavigationDirection.forward, animated: true)
}

This plain out doesn't work (I am trying to switch to suggestionPage) Another method I've tried is this:

PageViewController().transition(from: PageViewController().viewControllers![0], to: PageViewController().viewControllers![2], duration: 0.3, options: UIView.AnimationOptions.curveEaseInOut, animations: nil, completion: nil)

This gives me a Thread 1: signal SIGABRT error on my:

class AppDelegate: UIResponder, UIApplicationDelegate {

First line of code in my AppDelegate, I'd assume it's due to an unwrapping of an option issue? Does anyone know how I'm either using the functions wrong or have a better way of doing this?

Upvotes: 0

Views: 1633

Answers (2)

Martin
Martin

Reputation: 866

Change your code as given below. When you are writing PageViewController() it means you are creating new object and of type PageViewController and you are trying to set page on that object instance, which is not added to your view.

@objc func myOpinionAction(_ sender:UIButton!)
{
    pageViewController.setViewControllers([suggestionPage()], direction: UIPageViewController.NavigationDirection.forward, animated: true)
}

Upvotes: 0

Ashish
Ashish

Reputation: 116

Try this

func forward() {
        if currentPageIndex < totalPageIndex {
            pageController?.setViewControllers([viewControllerAtIndex(index: currentPageIndex + 1)], direction: .forward, animated: true, completion: nil)
        }
    }

    func backward() {
        if currentPageIndex > 0 {
            pageController?.setViewControllers([viewControllerAtIndex(index: currentPageIndex - 1)], direction: .reverse, animated: true, completion: nil)
        }
    }

Upvotes: 1

Related Questions