Reputation: 161
Swift language preferred.
I want UIPageViewController to have three viewControllers in it(check image). I do not know how to implement the three viewControllers into the UIPageViewController correctly, before i posted this question i have looked around on the forum for similar questions but i did not find any.
On the white viewController i have connected other viewControllers onto it, i basically just want the same swipe navigation like "Youtube" and "SnapChat".
I am very new to Xcode and if possible a bit a detailed explanation so i will understand the concept of it.(I already read the about UIPageViewController on Apple´s website.)
Swift language preferred. Thanks for helping.
Upvotes: 0
Views: 730
Reputation: 3759
There is many ways you can use it. This is how i prefer to do it.
class PagingController: UIViewController, UIPageViewControllerDataSource {
let pageController:UIPageViewController
//Initializing PageController
required init(coder aDecoder: NSCoder) {
pageController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
//Assingning the protocol inplemetation
pageController.dataSource = self
//Adding the view
if let initialViewController = viewControllerAtIndex(0) {
pageController.setViewControllers([initialViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated:false, completion: nil)
addChildViewController(pageController)
view .addSubview(self.pageController.view)
pageController.didMoveToParentViewController(self)
}
}
//Adding the next view controller
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let currentDay = ((viewController as! UINavigationController).viewControllers[0] as! DietDayTableViewController).currentDay
let nextDay = (currentDay + 1) % 7
return viewControllerAtIndex(nextDay)
}
//Adding the previous view controller
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let currentDay = ((viewController as! UINavigationController).viewControllers[0] as! DietDayTableViewController).currentDay
let previousDay = ((currentDay - 1) + 7) % 7
return viewControllerAtIndex(previousDay)
}
//A function witch gives me the right view controller for each page
func viewControllerAtIndex(index:Int) -> UINavigationController?{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyboard.instantiateViewControllerWithIdentifier("DietDayTableViewController") as? DietDayTableViewController {
vc.currentDay = index
return UINavigationController(rootViewController: vc)
}
return nil
}
}
I add comments for help. This pageController is for the days of the week. Each page is a day and i am using the same viewController for each page. The concept of pageviewcontroller is that you have asign the previous view controller and the next of the current one.
Upvotes: 1