我正在创建pageViewController内部createViewController()方法。我定义了UIPageViewController的一个属性self.pageController
由于重复的代码块,当这些页面打开时,当我们离开滚动页面时,它也会显示在视图后面。

func createPageController()
    {
        var deviceFrame:CGSize = UIScreen.mainScreen().bounds.size
        self.pageController = UIPageViewController(transitionStyle: UIPageViewControllerTransitionStyle.Scroll,
            navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal,
            options: [UIPageViewControllerOptionInterPageSpacingKey : 0])

            self.pageController!.view.frame = CGRectMake(0,108, deviceFrame.width, deviceFrame.height-110)
            self.dailyChallengeViewController = UIStoryboard(name: "Main_iPhone", bundle: nil).instantiateViewControllerWithIdentifier("DailyChallengeViewController") as? DailyChallengeViewController
            self.winnersViewController = UIStoryboard(name: "Main_iPhone", bundle: nil).instantiateViewControllerWithIdentifier("WinnersViewController") as? WinnersViewController

// All the below code will repeat 1 more time
 self.winnersViewController!.view.frame = self.pageController!.view.frame
        self.dailyChallengeViewController!.view.frame = self.pageController!.view.frame

        self.dailyChallengeViewController!.index = 0
        self.winnersViewController!.index = 1

        self.viewControllers = [self.dailyChallengeViewController!,self.winnersViewController!]
        self.pageController!.setViewControllers([self.dailyChallengeViewController!],
            direction:UIPageViewControllerNavigationDirection.Forward,
            animated:false,
            completion:nil)
        self.addChildViewController(self.pageController!)

        self.view.addSubview(self.pageController!.view)

        self.pageController!.dataSource = self
        self.pageController!.delegate = self

 self.pageController!.didMoveToParentViewController(self)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?{
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?{
}

最佳答案

据我所知,很多人都有这个问题。
这是因为viewControllerBeforeViewController和viewControllerAfterViewController。
建议使用willTransitionToViewControllers来获取PVC的当前索引以及上述的其他两种方法。
换句话说,如果希望在索引x处发生特定的事情,请在willTransitionToViewControllers委托方法中编写if语句。
希望这对你有帮助!
PageViewController delegate functions called twice

关于ios - 为什么PageViewController中的代码发生两次?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27152997/

10-12 05:20