将UIPageViewController与Swift和多个视图控制器一起使用
我正在尝试UIPageViewController
在具有多个视图控制器的Swift应用程序中使用。我的情节提要板(firstViewController
和secondViewController
)上有2个视图控制器,它们都嵌入各自的导航控制器中。它们具有情节提要标识符“
FirstViewController
”和“
SecondViewController
”。我在情节提要上还具有一个带有identifierPageView
控制器的页面视图控制器,并将导航设置为水平和过渡样式以在属性检查器中滚动。FirstViewController
是应用程序的根视图控制器
我想firstViewController
成为页面视图控制器的第一页,所以我这样编写了它的类:
import Foundationimport 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 Foundationimport 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
您只需单击并拖动到视图控制器的按钮,但是不幸的是事实并非如此。就像我说过我以前从未与其中之一合作过,所以我有点迷路了。
回答:
根据iiFreeman的建议,我继承了子类UIPageViewController
,并将其作为情节提要文件的根。下面是子类
import Foundationimport 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
因为我的页面视图控制器可以处理所有内容,所以我的其他控制器子类不需要任何东西。我意识到的一件事是,由于tableViewController
s嵌入在导航控制器中,因此我的页面视图控制器实际上需要显示导航控制器,而不是导航控件。tableView
控制器。下面是我的实现:
import Foundationimport 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
}
}
以上是 将UIPageViewController与Swift和多个视图控制器一起使用 的全部内容, 来源链接: utcz.com/qa/413398.html