Carousel situation

off-topic

(Thoughtcastdavid) #1

This seems like an easy issue but I'm not that good at swift.. Here's my code:

import UIKit

class SavedDrawingsController: UIPageViewController
{
    fileprivate lazy var pages: [UIViewController] = {
        return [
            self.getViewController(withIdentifier: "Page1"),
            self.getViewController(withIdentifier: "Page2")
        ]
    }()
    
    fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController
    {
        
       
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: identifier)
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.dataSource = self
        self.delegate   = self
        
        if let firstVC = pages.first
        {
            setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
        }
    }
}

extension SavedDrawingsController: UIPageViewControllerDataSource
{
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        
        guard let viewControllerIndex = pages.index(of: viewController) else { return nil }
        
        let previousIndex = viewControllerIndex - 1
        
        guard previousIndex >= 0          else { return pages.last }
        
        guard pages.count > previousIndex else { return nil        }
        
        return pages[previousIndex]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
    {
        guard let viewControllerIndex = pages.index(of: viewController) else { return nil }
        
        let nextIndex = viewControllerIndex + 1
        
        guard nextIndex < pages.count else { return pages.first }
        
        guard pages.count > nextIndex else { return nil         }
        
        return pages[nextIndex]
    }
}

extension SavedDrawingsController: UIPageViewControllerDelegate { }

Here's what I want:

  fileprivate lazy var pages: [UIViewController] = {
            return [
                self.getViewController(withIdentifier: "Page1"),
                self.getViewController(withIdentifier: "Page2")
            ]
        }()

instead of having to type Page1, Page2, etc... I'd like to have my getViewController function return

let vc = self.storyboard?.instantiateViewController(withIdentifier: "ScaledDrawingViewController") as! ScaledDrawingViewController!
vc?.drawingCount = page#

the vc the amount of times found in a variable drawings.collection.count

Am i making sense? Help please.


(Garth Snyder) #2

Could just be me, but I do not understand what you're trying to do. It's unclear even whether you are asking a Swift question or a UIKit question.