Reputation: 747
These are my view controllers:
I want to make it so that I can swipe between the three of them starting in the middle one. All of the tutorials I've found require you to start from scratch and don't show me how to connect the three I have. Does anyone have a step-by-step instruction of how to do this?
Any help would be greatly appreciated
Upvotes: 0
Views: 182
Reputation: 8396
First Create a class called PageViewController, drag a UIPageViewController
in storyboard.For now lets set it as initial view controller from attributes inspector. Also from identity inspector set PageViewController as Class.
Call your three view controller for example StepZero,StepOne,StepTwo Also give them identifier in storyboard.
lets deep into coding now, so in PageViewController should subclass UIPageVIewController
:
import UIKit
class PageViewController : UIPageViewController,UIPageViewControllerDataSource {
var selectedIndex = 1
override func viewDidLoad() {
dataSource = self
view.backgroundColor = UIColor.darkGrayColor()
// This is the starting point. Start with step zero.
setViewControllers([getStepOne()], direction: .Forward, animated: false, completion: nil)
}
func getStepZero() -> StepZero {
return storyboard!.instantiateViewControllerWithIdentifier("StepZero") as! StepZero
}
func getStepOne() -> StepOne {
return storyboard!.instantiateViewControllerWithIdentifier("StepOne") as! StepOne
}
func getStepTwo() -> StepTwo {
return storyboard!.instantiateViewControllerWithIdentifier("StepTwo") as! StepTwo
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(StepTwo) {
// 2 -> 1
return getStepOne()
} else if viewController.isKindOfClass(StepOne) {
// 1 -> 0
return getStepZero()
} else {
// 0 -> end of the road
return nil
}
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(StepZero) {
// 0 -> 1
return getStepOne()
} else if viewController.isKindOfClass(StepOne) {
// 1 -> 2
return getStepTwo()
} else {
// 2 -> end of the road
return nil
}
}
// Enables pagination dots
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return 3
}
// This only gets called once, when setViewControllers is called
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return selectedIndex
}
}
Let's say in Storyboard you have three viewControllers, you should set identifier for them from identity inspector as StepZero StepOne StepTwo for example and when you instantiate them you do :
func getStepZero() -> StepZero {
return storyboard!.instantiateViewControllerWithIdentifier("StepZero") as! StepZero
}
func getStepOne() -> StepOne {
return storyboard!.instantiateViewControllerWithIdentifier("StepOne") as! StepOne
}
func getStepTwo() -> StepTwo {
return storyboard!.instantiateViewControllerWithIdentifier("StepTwo") as! StepTwo
}
The selected index is the index you want to start with which is number 1. And to start with second view controller call getStepOne()
in setViewControllers
. if you want to start with view controller 3 use selected index 2 and call getStepTwo()
...etc
Download Updated Sample : https://mega.nz/#!EQEFhbwS!0yoy5RvAliQNnjRevWo05wPWk7P08e8DVetRZdjg-ro
Upvotes: 2
Reputation: 1112
Follow as below image
Give storyboard identifier as shown in below image
On button click push new viewcontroller
var viewControllerObj=self.storyboard!.instantiateViewControllerWithIdentifier("your storyboard identifier")
self.navigationController!.pushViewController(viewControllerObj, animated: true)
Upvotes: 0
Reputation: 1
You can connect your view controllers by navigation controller . Just select one of your view controllers -> Editor(on the top bar of mac) -> Embed in -> Navigation controller.
Also if you want to swipe you can use a scroll view and only on view controller . Scroll view with content size of 3 view controllers can help you do the same. Thank you
Upvotes: 0