为什么PageViewController中的代码发生了两次?

时间:2014-11-26 15:31:50

标签: ios objective-c swift

我正在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?{
}

1 个答案:

答案 0 :(得分:2)

据我所知,很多人都有这个问题。

这是因为 viewControllerBeforeViewController viewControllerAfterViewController

建议使用 willTransitionToViewControllers 获取PVC的当前索引以及上述其他两种方法。

换句话说,如果您希望在索引x处发生特定事情,请在 willTransitionToViewControllers 委托方法中编写if语句。

希望这能帮到你!

PageViewController delegate functions called twice