Reputation: 220
I am currently developing an iOS app with 3 View Controllers that are accessible by swiping right and left.
I want to have "WeatherVC" as the View Controller that is shown when the app is launched and I am struggling to figure this one out. I also want the specific order between the tabs to be: Tab 1 = ClimaVC - Tab 2 = WeatherVC - Tab 3 = ClosetVC.
Here is my code so far, with my 3 View Controllers:
import UIKit
class PageViewController: UIPageViewController,
UIPageViewControllerDelegate, UIPageViewControllerDataSource {
lazy var orderedViewControllers : [UIViewController] = {
return [self.newVC(viewController : "ClimaVC"),
self.newVC(viewController : "WeatherVC"),
self.newVC(viewController : "ClosetVC")]
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.dataSource = self
//If let to check if firstViewController is not nil to use it.
if let firstViewController = orderedViewControllers.first{
setViewControllers([firstViewController],
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 = 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
}
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
}
return orderedViewControllers[nextIndex]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Any help would be greatly appreciated as I cannot figure this one out & I spend quite some time trying!
Thanks guys!
Upvotes: 1
Views: 1127
Reputation: 41
Why not try UICollectionView? UIPageViewController have some issues, this case don't need UIPageViewController
Upvotes: 0
Reputation: 100523
You can try
setViewControllers([orderedViewControllers[1]],
direction: .forward, animated: true, completion: nil)
Upvotes: 1