Child ViewController Not Receiving Delegate Calls from Parent PageViewController - ios

I'm trying to send some data with a delegate call from a Parent PageViewController to a Child ViewController. However, at the moment, this doesn't work. I have my views set up like this:
ViewController (which contains a map view, in which data needs to be passed to the ParentPageVC)
ParentPageViewController (which then needs to pass the data received to ChildTwoVC)
ChildOneViewController
ChildTwoViewController (displays the data received from parent)
Interestingly, I can send data between the children inside the parent PageViewController. I can also send data from the initial ViewController to the ParentPageViewController just fine. I just cannot send the data from the Parent TO the Child.
Here's my Parent PageViewController:
protocol ParentPageViewDelegate: class {
func viewNeedsDismiss()
}
class ParentPageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource, ParentPageViewDelegate, MyMapDataViewDelegate {
lazy var childViewControllers = [UIViewController]()
weak var childTwoViewDelegate: ChildTwoViewDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
let childOneViewController = storyboard?.instantiateViewController(withIdentifier: "ChildOneViewController") as! ChildOneViewController
let childTwoViewController = storyboard?.instantiateViewController(withIdentifier: "ChildTwoViewController") as! ChildTwoViewController
childOneViewController.childOneDelegate = childTwoViewController
childTwoViewController.parentPageViewDelegate = self
self.childTwoViewDelegate = childTwoViewController
workoutViewControllers.append(childOneViewController)
workoutViewControllers.append(childTwoViewController)
if let firstViewController = workoutViewControllers.last {
setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
}
let pageControl = UIPageControl.appearance(whenContainedInInstancesOf: [StartWorkoutPageViewController.self])
pageControl.currentPageIndicatorTintColor = .orange
pageControl.pageIndicatorTintColor = .gray
}
// MARK: Delegates
// ParentPageView Delegate
func viewNeedsDismiss() {
// Works fine
print("WillDismiss")
}
// MyMapDataView Delegate
func updateStats(distance: String, rawDistance: Double, speedPace: String) {
print("Method Was Called") // Yes, the method is called.
// However nothing happens in the below line... it doesn't call 'childTwoViewDelegate'
childTwoViewDelegate?.updateStats(distance: distance, rawDistance: rawDistance, speedPace: speedPace)
}
// MARK: PageView DataSource
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = childViewControllers.firstIndex(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return childViewControllers.last
}
guard childViewControllers.count > previousIndex else {
return nil
}
return childViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = childViewControllers.firstIndex(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let childViewControllersCount = childViewControllers.count
guard childViewControllersCount != nextIndex else {
return workoutViewControllers.first
}
guard childViewControllersCount > nextIndex else {
return nil
}
return childViewControllers[nextIndex]
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return childViewControllers.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = workoutViewControllers.firstIndex(of: firstViewController) else {
return 0
}
return firstViewControllerIndex
}
}
My Child ViewController:
protocol ChildTwoViewDelegate: class {
func updateStats(distance: String, rawDistance: Double, speedPace: String)
}
class ChildTwoViewController: UIViewController, ChildTwoViewDelegate {
weak var parentPageViewDelegate: ParentPageViewDelegate?
#IBAction func closeButton(_sender: Any) {
// Works fine
parentPageViewDelegate?.viewNeedsDismiss()
}
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: Delegates
// ChildTwoViewDelegate
func updateStats(distance: String, rawDistance: Double, speedPace: String) {
// This method does not get called.
print("Method Was Called in Child")
}
}
Thank you!

I just stripped the parts out that I couldn't use and ran it in Xcode and it worked for me. I'll post the functioning part and maybe it'll help you deduce the problem. I don't see anything conspicuously wrong with your code. It may be a problem with the storyboard. I don't use storyboards so I can't comment on that.
protocol ParentPageViewDelegate: class {
func viewNeedsDismiss()
}
class ParentPageViewController: UIViewController, ParentPageViewDelegate {
// lazy var childViewControllers = [UIViewController]()
weak var childTwoViewDelegate: ChildTwoViewDelegate?
let childTwoViewController = ChildTwoViewController()
override func viewDidLoad() {
super.viewDidLoad()
// let childOneViewController = storyboard?.instantiateViewController(withIdentifier: "ChildOneViewController") as! ChildOneViewController
// let childTwoViewController = storyboard?.instantiateViewController(withIdentifier: "ChildTwoViewController") as! ChildTwoViewController
// childOneViewController.childOneDelegate = childTwoViewController
childTwoViewController.parentPageViewDelegate = self
self.childTwoViewDelegate = childTwoViewController
childTwoViewDelegate?.updateStats(distance: "test", rawDistance: 3.4, speedPace: "none")
// workoutViewControllers.append(childOneViewController)
// workoutViewControllers.append(childTwoViewController)
//
// if let firstViewController = workoutViewControllers.last {
// setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
// }
// let pageControl = UIPageControl.appearance(whenContainedInInstancesOf: [StartWorkoutPageViewController.self])
// pageControl.currentPageIndicatorTintColor = .orange
// pageControl.pageIndicatorTintColor = .gray
}
// MARK: Delegates
// ParentPageView Delegate
func viewNeedsDismiss() {
// Works fine
print("WillDismiss")
}
// MyMapDataView Delegate
func updateStats(distance: String, rawDistance: Double, speedPace: String) {
print("Method Was Called") // Yes, the method is called.
// However nothing happens in the below line... it doesn't call 'childTwoViewDelegate'
childTwoViewDelegate?.updateStats(distance: distance, rawDistance: rawDistance, speedPace: speedPace)
}
// MARK: PageView DataSource
// func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
// guard let viewControllerIndex = childViewControllers.firstIndex(of: viewController) else {
// return nil
// }
//
// let previousIndex = viewControllerIndex - 1
//
// guard previousIndex >= 0 else {
// return childViewControllers.last
// }
//
// guard childViewControllers.count > previousIndex else {
// return nil
// }
//
// return childViewControllers[previousIndex]
// }
//
// func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
// guard let viewControllerIndex = childViewControllers.firstIndex(of: viewController) else {
// return nil
// }
//
// let nextIndex = viewControllerIndex + 1
// let childViewControllersCount = childViewControllers.count
//
//// guard childViewControllersCount != nextIndex else {
//// return workoutViewControllers.first
//// }
//
// guard childViewControllersCount > nextIndex else {
// return nil
// }
//
// return childViewControllers[nextIndex]
// }
// func presentationCount(for pageViewController: UIPageViewController) -> Int {
// return childViewControllers.count
// }
// func presentationIndex(for pageViewController: UIPageViewController) -> Int {
// guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = workoutViewControllers.firstIndex(of: firstViewController) else {
// return 0
// }
//
// return firstViewControllerIndex
// }
}
protocol ChildTwoViewDelegate: class {
func updateStats(distance: String, rawDistance: Double, speedPace: String)
}
class ChildTwoViewController: UIViewController, ChildTwoViewDelegate {
weak var parentPageViewDelegate: ParentPageViewDelegate?
#IBAction func closeButton(_sender: Any) {
// Works fine
parentPageViewDelegate?.viewNeedsDismiss()
}
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: Delegates
// ChildTwoViewDelegate
func updateStats(distance: String, rawDistance: Double, speedPace: String) {
// This method does not get called.
print("Method Was Called in Child")
}
}
Let me know if this helps.

Related

UIPageViewControllerDataSource is not called

The UIPageViewControllerDataSource is not being called from ViewDidLoad, hence only the first page is displayed and swipe doesn't do anything.
I have followed this tutorial:
https://spin.atomicobject.com/2015/12/23/swift-uipageviewcontroller-tutorial/
The answers in questions similar to this do not work. I have viewed 5 similar questions but none of them work for me.
class SetupViewController: UIPageViewController {
private(set) lazy var pages : [UIViewController] = {
return [self.newPage(1), self.newPage(2), self.newPage(3), self.newPage(4)]
}()
private func newPage(_ page: Int) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Page\(page)")
}
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
if let firstPage = pages.first {
print(#function, "if let firstPage", firstPage, pages) //viewDidLoad() if let firstPage page1name [page1, page2, page3, page4]
setViewControllers([firstPage], direction: .forward, animated: true, completion: nil)
}
}
}
extension SetupViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
print(#function) //It doesn't print
guard let pageIndex = pages.firstIndex(of: viewController) else { return nil }
let nextIndex = pageIndex + 1
let pageCount = pages.count
guard nextIndex != pageCount else { return nil }
guard pageCount > nextIndex else { return nil }
return pages[nextIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
print(#function) //It doesn't print
guard let pageIndex = pages.firstIndex(of: viewController) else { return nil }
let previousIndex = pageIndex - 1
guard previousIndex >= 0 else { return nil }
guard pages.count > previousIndex else { return nil }
return pages[previousIndex]
}
}
Storyboard IDs are: Page1, Page2, Page3 & Page4
What am I doing wrong?
May be you have a configuration problem i have created a demo here
https://github.com/ShKhan9/PagerShow
let wind = (UIApplication.shared.delegate as! AppDelegate).window!
wind.rootViewController = SetupViewController()

Pass data in UIPageViewController between different swipe views

I am working on a project with 3 views that are accessed by swiping.
The following code implements the PageViewController I am using to handle the swiping motion:
import UIKit
class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
lazy var orderedViewControllers : [UIViewController] = {
return [self.newVC(viewController : "ClimaVC"),
self.newVC(viewController : "WeatherVC"),
self.newVC(viewController : "ClosetVC")]
}()
var myViewControllers = [UIViewController]()
var currentIndex : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.dataSource = self
setViewControllers([orderedViewControllers[1]],
direction: .forward, animated: true, completion: nil)
self.delegate = self
}
func newVC(viewController : String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1;
guard previousIndex >= 0 else {
// return orderedViewControllers.last
//Return nil to avoid swiping forever.
return nil
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
currentIndex = previousIndex
return orderedViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1;
guard orderedViewControllers.count != nextIndex else {
//return orderedViewControllers.first
//Return nil to avoid swiping forever.
return nil
}
guard orderedViewControllers.count > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
}
The main View Controller is WeatherVC. In this view, time data is retrieved and stored in an integer variable named timeOfDay.
Depending on the value of timeOfDay, the background displayed will differ. While I was able to use this variable in WeatherVC, I need to pass this variable timeOfDay to the two other view controllers ClimaVC and ClosetVC.
I tried setting up a delegate protocol but failed, explored using the different functions provided by UIPageViewControllerDelegate and it did not get me anywhere.
I would really appreciate some help on this,
Thank you very much!
It's not complicated, just assign the values at the pageviewcontroller datasources.
import UIKit
class ClimaViewController: UIViewController{
var timeOfDayClima: Int = 0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print (timeOfDayClima)
}
}
class WeatherViewController: UIViewController{
var timeOfDayClima: Int = 0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
timeOfDayClima += 1
}
}
class ClosetViewController: UIViewController{
var timeOfDayClima: Int = 0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print (timeOfDayClima)
}
}
class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
lazy var orderedViewControllers : [UIViewController] = {
return [self.newVC(viewController : "ClimaVC"),
self.newVC(viewController : "WeatherVC"),
self.newVC(viewController : "ClosetVC")]
}()
var myViewControllers = [UIViewController]()
var currentIndex : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.dataSource = self
setViewControllers([orderedViewControllers[1]],
direction: .forward, animated: true, completion: nil)
self.delegate = self
}
func newVC(viewController : String) -> UIViewController {
return UIStoryboard(name: "Second", bundle: nil).instantiateViewController(withIdentifier: viewController)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
//Guard = some kind of if statement.
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1;
guard previousIndex >= 0 else {
// return orderedViewControllers.last
//Return nil to avoid swiping forever.
return nil
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
currentIndex = previousIndex
if(currentIndex == 0) {
(orderedViewControllers.first as! ClimaViewController).timeOfDayClima = (orderedViewControllers[1] as! WeatherViewController).timeOfDayClima
}
return orderedViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
//Guard = some kind of if statement.
guard let viewControllerIndex = orderedViewControllers.index(of: viewController)
else {
return nil
}
let nextIndex = viewControllerIndex + 1;
guard orderedViewControllers.count != nextIndex else {
//return orderedViewControllers.first
//Return nil to avoid swiping forever.
return nil
}
guard orderedViewControllers.count > nextIndex else {
return nil
}
currentIndex = nextIndex
if(currentIndex == orderedViewControllers.count - 1) {
(orderedViewControllers.last as! ClosetViewController).timeOfDayClima = (orderedViewControllers[1] as! WeatherViewController).timeOfDayClima
}
if(nextIndex == 0) {
let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "ClimaVC") as! ClimaViewController
secondVC.timeOfDayClima = 1
}
return orderedViewControllers[nextIndex]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Maybe you should try explaining what has failed in setting up your delegate protocol as well because it seems like it is likely the solution you are looking for. I'm writing a protocol here for reference but you have to change it according to your needs.
protocol WeatherVCDelegate {
func changedTime(timeOfDay: Int)
}
Here comes the tricky part. When you declare a delegate in WeatherVC, you need to use the delegate to call the function to relay the timeOfDay to other weatherVC. How this works is that anything that conforms to the delegate protocol will be triggered to use the function.
class WeatherVC {
weak var delegate: WeatherVCDelegate?
func functionName() {
delegate?.changedTime(timeOfDay: value)
}
Now, when you initialize weatherVC in your PageViewController, you need PageViewController to conform to weatherVC delegate.
In PageViewController...
weatherVC.delegate = self
extension PageViewController: WeatherVCDelegate {
...
}
Here is more read up about delegates if my answer is not clear enough
https://medium.com/#jamesrochabrun/implementing-delegates-in-swift-step-by-step-d3211cbac3ef

page view controller hide indicator dots and stop in the last page (can't come back)

I have a page control in my project and I want to do two things :
1. I want to hide indicator dots in the last page
2. I want to lock the last page in the (page VC(my class)) that I can't come back to the first page
here is my code
class pageVC : UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate {
lazy var VCArr : [UIViewController] = {
return [self.VCInstance(name : "FirtsVC"),
self.VCInstance(name : "SecondVC"),
self.VCInstance(name :"ThirdVC"),
self.VCInstance(name :"FourthVC"),
self.VCInstance(name :"FivethVC")]
}()
private func VCInstance(name : String) -> UIViewController {
return UIStoryboard(name : "Main" , bundle : nil).instantiateViewController(withIdentifier: name)
}
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if VCArr.last != nil {
print("first Page Reached!")
}
if let firstVC = VCArr.first {
setViewControllers([firstVC] , direction: .forward , animated: true, completion: nil)
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let pageVC = UIPageControl()
for view in self.view.subviews {
if view is UIScrollView {
view.frame = UIScreen.main.bounds
}else if view is UIPageControl{
view.backgroundColor = UIColor.clear
pageVC.numberOfPages = 5
pageVC.center = self.view.center
pageVC.layer.position.y = self.view.frame.height - 180 ;
}
}
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = VCArr.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex-1
guard previousIndex >= 0 else {
return VCArr.last
}
guard VCArr.count > previousIndex else {
return nil
}
return VCArr[previousIndex]
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = VCArr.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex+1
guard nextIndex < VCArr.count
else {
return VCArr.first
}
guard VCArr.count > nextIndex else {
return nil
}
return VCArr[nextIndex]
}
public func presentationCount(for pageViewController: UIPageViewController) -> Int{
return VCArr.count
}
public func presentationIndex(for pageViewController: UIPageViewController) -> Int{
guard let firstViewController = viewControllers?.first , let firstViewControllerIndex = VCArr.index(of: firstViewController) else {
return 0
}
return firstViewControllerIndex
}
public func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if (VCArr.last!.isViewLoaded)
{
print("It is Done!!!")
}
}
Change this, which is making the page view to return to the first one when it is in the last one:
let nextIndex = viewControllerIndex+1
guard nextIndex < VCArr.count
else {
return nil //was VCArr.first
}
And to make it hidden, first move the let pageVC = UIPageControl() below the class pageVC : UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate { to make it public within the class. Then add:
if nextIndex == VCArr.count{
pageVC.isHidden = true
}

PageViewController after go to View Controller

I have a pageViewController with 3 viewControllers which is embedded in a view of a viewcontroller.
i am setting the view controllers programatically:
lazy var vcArr: [UIViewController] = {
return [self.vcInstance(name: "vc1"),
self.vcInstance(name: "vc2"),
self.vcInstance(name: "vc3"),
]
}()
i am programatically setting the number of pages to 4
let pageController = UIPageControl.appearance()
pageController.numberOfPages = 4
what i want to happen is that if the user tries to swipe right from the 3rd page it will segue to a signup viewController.
I'm currently trying to achieve this in the viewcontroller after function testing for it the next item is greater than or equal to the total count.
public func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = vcArr.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
if nextIndex >= vcArr.count {
infoClick()
return nil
} else {
guard nextIndex < vcArr.count else {
return self.vcArr.last
}
guard self.vcArr.count > nextIndex else {
return nil
}
return self.vcArr[nextIndex]
}
}
with this test the way it is sometimes it stops on page 3 and then when swiped right it will segue off with my function infoClick(). however somethings it goes right off when it hits the 3rd page as you would think because its > or = the number of pages which is 3.
when i change this to just > it doesn't display anything on the third page.
I've searched heaps for a way around this but to no avail. does anyone have any ideas how to achieve this?
attaching entire class below:
import UIKit
import Parse
class pageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
#IBOutlet var createAccountBarButton: UIBarButtonItem!
lazy var vcArr: [UIViewController] = {
return [self.vcInstance(name: "vc1"),
self.vcInstance(name: "vc2"),
self.vcInstance(name: "vc3"),
]
}()
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
//---------------------------------------------- autoLogin
/// temporary location, needs to be launched during the loading screen
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
// if (PFUser.current() != nil) {
// let vc = self.storyboard!.instantiateViewController(withIdentifier: "PackViewController")
// self.present(vc, animated: true, completion: nil)
// if (PFUser.current() != nil) {
// let tbc = self.storyboard!.instantiateViewController(withIdentifier: "MyTabController") as! UITabBarController
// tbc.selectedIndex = 1
// self.present(tbc, animated: true, completion: nil)
// }
}
//---------------------------------------------- set the viewcontrollwe instance
private func vcInstance(name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
}
//---------------------------------------------- set the direction and first page of the page controller
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.tabBar.isHidden = true
let pageController = UIPageControl.appearance()
pageController.pageIndicatorTintColor = UIColor.lightGray
pageController.currentPageIndicatorTintColor = GeneralFunctions.UIColorFromHEX(hexValue: 0xbb0d2a)
pageController.backgroundColor = UIColor.clear
pageController.bounds = CGRect(x: 0, y: 0, width: 0, height: 0)
pageController.numberOfPages = 4
//GeneralFunctions.commonButtonSettings(buttonName: signUpButton, hexValue: 0x0c1537)
self.dataSource = self
self.dataSource = self
if let firstVC = vcArr.first {
setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
}
}
//---------------------------------------------- page view controller before settings
public func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = vcArr.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
if previousIndex < 0 {
return nil
} else {
guard previousIndex >= 0 else {
return self.vcArr.last
}
guard self.vcArr.count > previousIndex else {
return nil
}
return self.vcArr[previousIndex]
}
}
//---------------------------------------------- page view controller after settings
public func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = vcArr.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
if nextIndex >= vcArr.count {
infoClick()
return nil
} else {
guard nextIndex < vcArr.count else {
return self.vcArr.last
}
guard self.vcArr.count > nextIndex else {
return nil
}
return self.vcArr[nextIndex]
}
}
//---------------------------------------------- presentation count
public func presentationCount(for pageViewController: UIPageViewController) -> Int {
return self.vcArr.count
}
//---------------------------------------------- presentation index
public func presentationIndex(for pageViewController: UIPageViewController) -> Int {
guard let firstViewController = viewControllers?.first,
let firstViewControllerIndex = vcArr.index(of: firstViewController) else {
return 0
}
return firstViewControllerIndex
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//---------------------------------------------- launch the new view controller - still launching before swipped to 4th window sometimes
// not sure if this is the best way to do this need to look at this a bit more
func infoClick() {
let storyboard: UIStoryboard = UIStoryboard (name: "Main", bundle: nil)
let vc: SignupViewController = storyboard.instantiateViewController(withIdentifier: "SignupViewController") as! SignupViewController
let currentController = getCurrentViewController()
currentController?.present(vc, animated: false, completion: nil)
}
func getCurrentViewController() -> UIViewController? {
if let rootController = UIApplication.shared.keyWindow?.rootViewController {
var currentController: UIViewController! = rootController
while( currentController.presentedViewController != nil ) {
currentController = currentController.presentedViewController
}
return currentController
}
return nil
}
}

present walkthrough scenes only once ios swift

I created a PageViewController and added three view controllers as a walkthrough for my app, but i want them to be displayed only once, and i can't figure out how.
here is my code for the pageViewController:
import UIKit
class MyPageViewController: UIPageViewController{
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
// Do any additional setup after loading the view.
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .Forward,
animated: true,
completion: nil)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//array for the ordered view controllers
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newColoredViewController("First"),
self.newColoredViewController("Second"),
self.newColoredViewController("Third")]
}()
private func newColoredViewController(color: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewControllerWithIdentifier("\(color)ViewController")
}
}
extension MyPageViewController: UIPageViewControllerDataSource {
//returning index of previous view controller
func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return nil
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
//returning index of the next view controller
func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
guard orderedViewControllersCount != nextIndex else {
return nil
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
}
how do i edit the code to display it only once?
From what you've said I'm guessing that you only want it to show the first time the user opens the app and then not appear again?
Theres a few ways of doing this, the simplest would likely be to set a property on NSUserDefaults, this will remember the setting in between app launches. try something like:
// this should be your initial view controller
let defaults = NSUserDefaults.standardUserDefaults()
override func viewDidLoad() {
super.viewDidLoad()
if !defaults.boolForKey("walkthroughSeen") {
// create page view controller and display
let walkthroughVC = UIStoryboard(name: "Main", bundle: nil).
instantiateViewControllerWithIdentifier("MyPageViewController")
self.presentViewController(walkthroughVC, animated: true)
defaults.setBool(true, forKey: "walkthroughSeen")
}
}

Resources