Reputation: 131
Relatively new to Xcode/Swift
, but have some experience in other languages.
I am at the infancy stages of an application and am trying to achieve the following functionality:
In a UIPageView
environment, when the user swipes forward, the label (whose value is an integer) increases by 1. When the user swipes back, the label decreases by 1.
I have a single UIViewController
as the content page with storyboard ID PageContentViewController
.
Essentially, my thoughts are to declare an integer variable initially set to 0 and then create a swipedBack()
and swipedRight()
function which updates the value of the variable and then sets the text of the label on the PageContentViewController
to the value of that variable. The functions then return the new PageContentViewController
.
I am getting some peculiar behaviour. Namely, the app is building and running fine.
The initial screen loads with a label of 0. Then I swipe forward, and it updates to 1. BUT THEN... if i swipe forward again, the label stays as one. BUT THEN... if I keep going forward, it then acts as desired - goes to 2, then 3. Then if i reverse direction, it becomes even more peculiar.
If I'm at a value of four, then swipe back, It will decrease to 3. But then swiping back again it will go back to 4, then continuing swiping, it will behave as expected (4,3,2,1...). I obviously have a conceptual misunderstanding and would appreciate some help.
Here's my code:
import UIKit
class ViewController: UIPageViewController, UIPageViewControllerDataSource {
var arrPageTitle: NSArray = NSArray()
var t: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.setViewControllers([getViewControllerAtIndex()] as [UIViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
return swipedBack()
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
return swipedForward()
}
func getViewControllerAtIndex() -> PageContentViewController {
let pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageContentViewController") as! PageContentViewController
pageContentViewController.strTitle = "\(t)"
return pageContentViewController
}
func swipedForward() -> PageContentViewController {
let pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageContentViewController") as! PageContentViewController
t = t + 1
pageContentViewController.strTitle = "\(t)"
return pageContentViewController
}
func swipedBack() -> PageContentViewController {
let pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageContentViewController") as! PageContentViewController
t = t - 1
pageContentViewController.strTitle = "\(t)"
return pageContentViewController
}
}
Upvotes: 0
Views: 161
Reputation: 3789
I think this is probably because once you change directions, the most recent UIViewController is still in memory, and thus the delegate function is not called.
I would recommend using the pageViewControllerWillTransitionTo
function instead. This should be called every time you swipe. Similarly you could use pageViewControllerDidFinishAnimating
if you want the call to happen after you swipe.
In order to detect which direction you swiped, you could try and use a UISwipeGestureRecognizer at the same time, and call functions that will increment/decrement the variable value. Actually, now that I think about it, you may just be able to use this gesture recognizer without the delegate function. Up to you.
Upvotes: 1