我有一个显示页面视图控制器的容器视图。然后,该视图控制器显示2个相同的视图控制器,我可以来回翻页。
import UIKit
class ViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pages = [UIViewController]()
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page1")
let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page2")
pages.append(page1)
pages.append(page2)
setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
let previousIndex = abs((currentIndex - 1) % pages.count)
print("previous Index", pages[previousIndex])
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
let nextIndex = abs((currentIndex + 1) % pages.count)
print("next Index", pages[nextIndex])
return pages[nextIndex]
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return pages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
我想做的是只创建一个视图控制器,然后使用数组中的数据填充该视图控制器
阵列:
var pageTitles : Array<String> = ["God vs Man", "Cool Breeze", "Fire Sky"]
var pageImages : Array<String> = ["page1.png", "page2.png", "page3.png"]
容器视图控制器是否可以显示页面视图控制器?
任何帮助将不胜感激......
Denis Angell
答案 0 :(得分:0)
如果您只想管理一个包含多个数据和UIPageController
的控制器,您应该创建一个视图控制器并执行类似的操作:
class PageViewController: UIViewController { // Id: 'PageViewController'
// MARK: @IBOutlet
/// Menu button item.
@IBOutlet weak var image: UIImageView!
/// Pagination controller.
@IBOutlet weak var pagination: UIPageControl!
// MARK: Variable
struct Page {
var title: String
var image: String
init(title: String, image: String) {
self.title = title
self.image = image
}
}
let data: [Page] = []
var currentIndex = 0
override func viewDidLoad() {
// Init data
data.append(Page(title: "title1" , image: "page1.png"))
data.append(Page(title: "title2" , image: "page2.png"))
setValues(currentIndex)
// Setup slide handler
let leftSwipe = UISwipeGestureRecognizer(target: self,
action: #selector(PageViewController(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self,
action: #selector(PageViewController(_:)))
leftSwipe.direction = .left
rightSwipe.direction = .right
self.view.addGestureRecognizer(leftSwipe)
self.view.addGestureRecognizer(rightSwipe)
}
/**
Handle swipes allows to switch page.
*/
func handleSwipes(_ sender: UISwipeGestureRecognizer) {
let size = self.data.count
let currentPosition = self.pagination.currentPage
if sender.direction == .left && currentPosition < size-1 {
nextPage()
} else if sender.direction == .right && currentPosition > 0 {
previousPage()
}
}
private func setValues(index: Int) {
self.pagination.currentPage = currentIndex
self.title = pageTitles[index]
self.image = pageImages[index]
}
// Button or see UIPageControl @IBOutlet
private func nextPage() {
currentIndex = abs((currentIndex + 1) % pages.count)
print("next Index", pages[currentIndex])
setValues(currentIndex)
}
// Button or see UIPageControl @IBOutlet
private func previousPage() {
currentIndex = abs((currentIndex - 1) % pages.count)
print("next Index", pages[currentIndex])
setValues(currentIndex)
}
// Other functions...
// ...
}