Change pages in UIPageViewController Automatically with timer in fast 3?

I have a UIPageViewController that contains 4 view controllers

I set a timer to automatically change the view controllers (go to the next view controllers). This timer and method will work. But the problem is that it works. Only for the first slide and when the application starts, and after 5 seconds the UIPageViewController will show the second slide, but nothing will happen after that! This is the code I used

 lazy var VCArr : [UIViewController] = { return [self.VCInstance(name : "VC1"), self.VCInstance(name : "VC2"), self.VCInstance(name :"VC3"), self.VCInstance(name :"VC4")] }() func changeSlide() { print("Slide Change!") guard let currentViewController = self.VCArr.first else { return } guard let nextViewController = dataSource?.pageViewController( self, viewControllerAfter: currentViewController ) else { return } setViewControllers([nextViewController], direction: .forward, animated: true, completion: nil) } var tTime: Timer! override func viewDidLoad() { super.viewDidLoad() tTime = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(changeSlide), userInfo: nil, repeats: true) } 

I think the problem is in this line

 guard let currentViewController = self.VCArr.first else { return } 

But I do not know how to change this line to work fine

+3
ios timer uiviewcontroller swift3 uipageviewcontroller
source share
1 answer

Try the following:

You can change the view controller by the VCArr index.

 var index = 0 func changeSlide() { index + 1 if index < self.VCArr.count { setViewControllers([VCArr[index]], direction: .forward, animated: true, completion: nil) } else { index = 0 setViewControllers([VCArr[0]], direction: .forward, animated: true, completion: nil) } } 
+2
source share

All Articles