我正在尝试UIPageViewController在具有多个视图控制器的Swift应用程序中使用。我的情节提要板(firstViewController和secondViewController)上有2个视图控制器,它们都嵌入各自的导航控制器中。它们具有情节提要标识符“ FirstViewController”和“ SecondViewController”。我在情节提要上还具有一个带有identifierPageView控制器的页面视图控制器,并将导航设置为水平和过渡样式以在属性检查器中滚动。FirstViewController是应用程序的根视图控制器
UIPageViewController
firstViewController
secondViewController
FirstViewController
SecondViewController
identifierPageView
我想firstViewController成为页面视图控制器的第一页,所以我这样编写了它的类:
import Foundation import UIKit class FirsttViewController: UITableViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate { override func viewDidLoad() { let pageViewController: PageViewController = self.storyboard.instantiateViewControllerWithIdentifier("PageViewController") as PageViewController pageViewController.delegate = self var viewControllers: [UIViewController] = [self] pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil) pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height) self.addChildViewController(pageViewController) self.view.addSubview(pageViewController.view) pageViewController.didMoveToParentViewController(self) } func pageViewController(pageViewController: UIPageViewController!, viewControllerAfterViewController viewController: UIViewController!) -> UIViewController! { let secondViewController: SecondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController return secondViewController } func pageViewController(pageViewController: UIPageViewController!, viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController! { return nil } func presentationCountForPageViewController(pageViewController: UIPageViewController!) -> Int { return 2 } func presentationIndexForPageViewController(pageViewController: UIPageViewController!) -> Int { return 0 } }
的文件SecondViewController如下所示:
import Foundation import UIKit class SecondViewController: UITableViewController, UIPageViewControllerDataSource { func pageViewController(pageViewController: UIPageViewController!, viewControllerAfterViewController viewController: UIViewController!) -> UIViewController! { return nil } func pageViewController(pageViewController: UIPageViewController!, viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController! { let firstViewController: FirstViewController = self.storyboard.instantiateViewControllerWithIdentifier("FirstViewController") as FirstViewController return firstViewController } }
但是,当我运行我的应用程序时,它似乎没有页面视图控制器。它只是显示firstViewController而已。没有点,没有页面等。有人知道怎么了吗?我找到了一些Objective- C教程,但它们都涵盖了对所有页面使用一个视图控制器的问题,并且具有一个不是页面的根视图控制器,pageView因此它们并没有太多帮助。我希望它就像使用一个tabBarController您只需单击并拖动到视图控制器的按钮,但是不幸的是事实并非如此。就像我说过我以前从未与其中之一合作过,所以我有点迷路了。
pageView
tabBarController
根据iiFreeman的建议,我继承了子类UIPageViewController,并将其作为情节提要文件的根。下面是子类
import Foundation import UIKit class PageViewController: UIPageViewController { override func viewDidLoad() { let startingViewController = self.storyboard.instantiateViewControllerWithIdentifier("FirstViewController") as FirstViewController self.dataSource = startingViewController var viewControllers: [UIViewController] = [startingViewController] self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil) } }
但是,现在该应用程序只是挂在黑屏上,最终Xcode失去了与模拟器的连接。我不太确定怎么回事。
好,我知道了。由于我需要页面视图控制器作为根并处理所有内容,因此我将其子类UIPageViewController化为iiFreeman建议并遵循委托和数据源协议。然后,在中设置控制器viewDidLoad,并实现委托和数据源方法。我添加了一个属性存储,其中包含一个故事板标识符数组,以及一个用于跟踪该数组当前索引的数组。我还添加了一个辅助方法,以给定索引返回正确的视图控制器。tableView因为我的页面视图控制器可以处理所有内容,所以我的其他控制器子类不需要任何东西。我意识到的一件事是,由于tableViewControllers嵌入在导航控制器中,因此我的页面视图控制器实际上需要显示导航控制器,而不是导航控件。tableView控制器。下面是我的实现:
viewDidLoad
tableView
tableViewController
import Foundation import UIKit class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate { var index = 0 var identifiers: NSArray = ["FirstNavigationController", "SecondNavigationController"] override func viewDidLoad() { self.dataSource = self self.delegate = self let startingViewController = self.viewControllerAtIndex(self.index) let viewControllers: NSArray = [startingViewController] self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil) } func viewControllerAtIndex(index: Int) -> UINavigationController! { //first view controller = firstViewControllers navigation controller if index == 0 { return self.storyboard.instantiateViewControllerWithIdentifier("FirstNavigationController") as UINavigationController } //second view controller = secondViewController's navigation controller if index == 1 { return self.storyboard.instantiateViewControllerWithIdentifier("SecondNavigationController") as UINavigationController } return nil } func pageViewController(pageViewController: UIPageViewController!, viewControllerAfterViewController viewController: UIViewController!) -> UIViewController! { let identifier = viewController.restorationIdentifier let index = self.identifiers.indexOfObject(identifier) //if the index is the end of the array, return nil since we dont want a view controller after the last one if index == identifiers.count - 1 { return nil } //increment the index to get the viewController after the current index self.index = self.index + 1 return self.viewControllerAtIndex(self.index) } func pageViewController(pageViewController: UIPageViewController!, viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController! { let identifier = viewController.restorationIdentifier let index = self.identifiers.indexOfObject(identifier) //if the index is 0, return nil since we dont want a view controller before the first one if index == 0 { return nil } //decrement the index to get the viewController before the current one self.index = self.index - 1 return self.viewControllerAtIndex(self.index) } func presentationCountForPageViewController(pageViewController: UIPageViewController!) -> Int { return self.identifiers.count } func presentationIndexForPageViewController(pageViewController: UIPageViewController!) -> Int { return 0 } }