gpt4 book ai didi

ios - Swift - 在一个 UIViewController 中实现 PageViewController

转载 作者:行者123 更新时间:2023-11-28 05:32:33 28 4
gpt4 key购买 nike

互联网上没有任何地方解释如何在 UIViewController 中实现 PageViewController我从默认的 XCode 基于页面的应用程序中获取此代码

class ViewController: UIViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
var pageViewController: UIPageViewController?
override func viewDidLoad() {
self.pageViewController = UIPageViewController(transitionStyle: .PageCurl, navigationOrientation: .Horizontal, options: nil)
self.pageViewController!.delegate = self
self.view.addSubview(self.pageViewController!.view)
// Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
var pageViewRect = self.view.bounds
self.pageViewController!.view.frame = pageViewRect
self.pageViewController!.didMoveToParentViewController(self)
// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
self.view.gestureRecognizers = self.pageViewController!.gestureRecognizers
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
//?? what controller should I return
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
//?? what controller should I return
}
}

我想要 6 个页面,我想在不同的页面中添加文本,如何识别我在哪个页面并显示我想要的信息?我只想使用 1 个 ViewControler,只有这个 View Controller 。

最佳答案

UIPageViewController 要求您使用不同的 View Controller 。您可能需要重构您的代码以拥有一个“父”或“根” View Controller ,其唯一的工作是呈现其他 View Controller ,这些 View Controller 实现了特定页面的逻辑。这是一些示例代码。它过于简单,但应该让您了解如何开始。

class PageContent {
let text: String
let index: Int
init(text: String, index: Int) {
self.text = text
self.index = index
}
}

class MyPageViewController : UIViewController {
var page: PageContent!
override func viewDidLoad() {
// Load page data for self.page
}
}

class ViewController: UIViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
var pageViewController: UIPageViewController?
var pages: [PageContent]!

override func viewDidLoad() {
let firstPage = PageContent(text: "...", index: 0)
let secondPage = PageContent(text: "...", index: 1)
// ...
self.pages = [firstPage, secondPage] // add more as necessary
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let beforeController = viewController as MyPageViewController
let newPageIndex = beforeController.page.index - 1
if newPageIndex >= 0 {
let newController = MyPageViewController()
newController.page = self.pages[newPageIndex]
return newController
} else {
return nil
}
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let afterController = viewController as MyPageViewController
let newPageIndex = afterController.page.index + 1
if newPageIndex < self.pages.count {
let newController = MyPageViewController()
newController.page = self.pages[newPageIndex]
return newController
} else {
return nil
}
}
}

关于ios - Swift - 在一个 UIViewController 中实现 PageViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27214295/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com