2017-02-07 11 views
0

以下の関数は、ユーザーがスワイプしたときに異なるビューを表示する方法です。 self.viewControllerAtIndex()はビューを返す独自のカスタム関数です。問題は、最初のスワイプが "--------- 0の直前にスワイプ"を2回出力することです。そして、その後は期待どおりに動作します。UIPageViewController最初のスワイプが2回呼び出されました

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { 
    print("---------swipe Right before " + String(index)) 
    index += 1 
    print("---------swipe Right " + String(index)) 

    if index == (products!.count) { 
     index = 0 
    } 

    return self.viewControllerAtIndex(index:index) 

} 
=====CONSOLE OUTPUT===== 
---------swipe Right before 0 
---------swipe Right 1 
---------swipe Right before 0 
---------swipe Right 1 

func viewControllerAtIndex(index: Int) -> ViewController{ 

    return ViewController.init(id: index) 
} 

何らかの理由で、最初の後に1回おきにスワイプすると、期待どおりに機能します。最初のスワイプは上記のコンソール出力を引き起こします。これは

First Swipe 
View1 
Second Swipe 
View1 
Third Swipe 
View2 
Fourth Swipe 
View1 
Fifth Swipe 
View2 

は私もだから私は、その後ときのviewDidLoad上に新しいのViewControllerを作成していますので、

let array = [ViewController](repeating: ViewController.init(videos: (self.videos![self.products![self.index].id]!)), count: 1) 

self.UIViewPageController?.setViewControllers(array, direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil) 

ように私のuiPageViewControllerを開始しています(2つのビュー)以下のように見えるように私のビューシーケンスを引き起こしユーザーのスワイプ、私は新しいViewControllerを作成しています

+0

私の答えはこちらをご覧ください。 http://stackoverflow.com/a/42032487/341994 – matt

答えて

0

私はこの方法でインデックス状態管理をしないでください。アップルは、この方法が呼ばれる状況を保証するものではありません。言い換えれば、フレームワークが提供されたView Controllerを要求している間に、別の質問に答えているとします。

この問題を回避するには、元のビューコントローラの配列を使用し、渡されたビューコントローラのインデックスをその配列に配置し、次に表示されるビューコントローラを返します。配列の終わり)。

関連する問題