Argument labels '(_:, viewControllerAfterViewController:)' do not match any available overloads

Learning swift at the moment, any help would be appreciated - Updating code from swift 3 to swift 4 - getting the following error when building:

Argument labels '(_:, viewControllerAfterViewController:)' do not match any available overloads

I believe the code that is relevant to this error is in the goToNextVC func below:

     func goToNextVC() {
     let nextVC = pageViewController(self, viewControllerAfterViewController: viewControllers![0] )!
    setViewControllers([nextVC], direction: .Forward, animated: true, completion: nil)
}



    extension ViewController: UIPageViewControllerDataSource {

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    // navigate right
    switch viewController {
    case cardsVC:
        return profileVC
    case profileVC:
        return nil
    case matchesVC:
        return cardsVC
    default:
        return nil
    }
}

full code:

    // sets up page view controller
    let pageController = ViewController(transitionStyle: .scroll,         navigationOrientation: .horizontal, options: nil)

    class ViewController: UIPageViewController {

    // page view controllers here
    let cardsVC: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CardsNavController")
    let profileVC: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ProfileNavController")
    let matchesVC: UIViewController! = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MatchesNavController")

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor.white
    dataSource = self
    // loads first view
    setViewControllers([cardsVC], direction: .forward, animated: true, completion: nil)
}

// load left view
func goToNextVC() {
    let nextVC = pageViewController(self, viewControllerAfterViewController: viewControllers![0] )!
    setViewControllers([nextVC], direction: .Forward, animated: true, completion: nil)
}
// load right view
func goToPreviousVC() {
    let previousVC = pageViewController(self, viewControllerBeforeViewController: viewControllers![0] )!
    setViewControllers([previousVC], direction: .Reverse, animated: true, completion: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


    }

    // MARK - UIPageViewControllerDataSource
    extension ViewController: UIPageViewControllerDataSource {

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    // navigate right
    switch viewController {
    case cardsVC:
        return profileVC
    case profileVC:
        return nil
    case matchesVC:
        return cardsVC
    default:
        return nil
    }
}

// navigate left
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    switch viewController {
    case cardsVC:
        return matchesVC
    case profileVC:
        return cardsVC
    case matchesVC:
        return nil
    default:
        return nil
    }
}

}

This:

let nextVC = pageViewController(self, viewControllerAfterViewController: viewControllers![0] )!

needs to be:

let nextVC = pageViewController(self, viewControllerAfter: viewControllers![0] )!

And while you are at it, you need to get rid of the forced unwrap (the !). pageViewController(_:viewControllerAfter:) can return nil and the use of ! will cause your app to crash.

func goToNextVC() {
    if let viewControllers = viewControllers {
        if let nextVC = pageViewController(self, viewControllerAfter: viewControllers[0]) {
            setViewControllers([nextVC], direction: .Forward, animated: true, completion: nil)
        }
    }
}

0 Comment

NO COMMENTS

LEAVE A REPLY

Captcha image