我目前正在开发具有3个视图控制器的iOS应用,可通过左右滑动来访问它们。
我希望将“ WeatherVC”作为启动应用程序时显示的View Controller,而我正在努力解决这一问题。 我还希望选项卡之间的特定顺序为:选项卡1 = ClimaVC-选项卡2 = WeatherVC-选项卡3 = ClosetVC。
到目前为止,这是我的代码,其中包含3个View Controller:
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.
}
}
任何帮助将不胜感激,因为我无法弄清楚这一点,而且我花了很多时间尝试!
谢谢大家!
答案 0 :(得分:1)
您可以尝试
setViewControllers([orderedViewControllers[1]],
direction: .forward, animated: true, completion: nil)
答案 1 :(得分:0)
为什么不尝试UICollectionView? UIPageViewController存在一些问题,这种情况下不需要UIPageViewController