我正在尝试为我的应用程序创建一个教程/简介,因为到现在为止,我不确定我决定将UIPageViewController与一个孩子一起使用会导致多少页面。
我的意图是将要显示的所有数据存储在对象数组中,并使用UIPageViewController中当前显示视图的索引从该数组中获取所需的对象。
override func viewWillAppear(_ animated: Bool)
{
print("PageContentVC > viewWillAppear")
ivPageContent.image = UIImage(named: DataSingleton.sharedInstance.returnTutorialVCContent()[contentPage].contentImage!)
lblPageContentTitle.text = DataSingleton.sharedInstance.returnTutorialVCContent()[contentPage].contentLabel!
lblPageContentText.text = DataSingleton.sharedInstance.returnTutorialVCContent()[contentPage].contentText!
}
为此,我需要将该索引从UIPageViewController传递给其子级ContentPageVC。 在获取索引方面没有问题,但是在将其传递给ContentPageVC时遇到困难。
import UIKit
class TutorialPageVC : UIPageViewController,
UIPageViewControllerDataSource,
UIPageViewControllerDelegate
{
/*
// MARK: - Variables & Constants
*/
var contentPage : Int = 0
var collectionViewPageControl : UIPageControl = UIPageControl()
lazy var pagesArray : [UIViewController] =
{
return [self.VCInstance(name: "pageContent"),
self.VCInstance(name: "pageContent"),
self.VCInstance(name: "pageContent"),
self.VCInstance(name: "pageContent"),
self.VCInstance(name: "pageContent"),
self.VCInstance(name: "pageContent"),
self.VCInstance(name: "pageContent"),
self.VCInstance(name: "pageContent"),
self.VCInstance(name: "pageContent")]
}()
private func VCInstance(name: String) -> UIViewController
{
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
}
/*
// MARK: - VC Status
*/
override func viewDidLoad()
{
super.viewDidLoad()
print("TutorialPageVC > viewDidLoad")
self.dataSource = self
self.delegate = self
if let firstVC = pagesArray.first
{
setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
}
let pageControl = UIPageControl.appearance()
pageControl.currentPageIndicatorTintColor = UIColor.activeColor
pageControl.pageIndicatorTintColor = UIColor.backgroundColor
self.view.backgroundColor = UIColor.clear
}
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
print("TutorialPageVC > viewDidLayoutSubviews")
for view in self.view.subviews
{
if view is UIScrollView { view.frame = UIScreen.main.bounds }
}
}
/*
// MARK: - UIPageViewController Delegates
*/
public func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController?
{
print("TutorialPageVC > pageViewController > viewControllerBefore")
guard let viewControllerIndex = pagesArray.firstIndex(of: viewController) else { return nil }
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else { return pagesArray.last }
guard pagesArray.count > previousIndex else { return nil }
return pagesArray[previousIndex]
}
public func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController?
{
print("TutorialPageVC > pageViewController > viewControllerAfter")
guard let viewControllerIndex = pagesArray.firstIndex(of: viewController) else { return nil }
let nextIndex = viewControllerIndex + 1
guard nextIndex < pagesArray.count else { return pagesArray.first }
guard pagesArray.count > nextIndex else { return nil }
return pagesArray[nextIndex]
}
public func presentationCount(for pageViewController: UIPageViewController) -> Int
{
print("TutorialPageVC > presentationCount")
return pagesArray.count
}
public func presentationIndex(for pageViewController: UIPageViewController) -> Int
{
print("TutorialPageVC > presentationIndex")
guard let firstViewController = viewControllers?.first,
let firstViewControllerIndex = pagesArray.firstIndex(of: firstViewController) else
{
return 0
}
return firstViewControllerIndex
}
public func pageViewController(_ pageViewController: UIPageViewController,
willTransitionTo pendingViewControllers: [UIViewController])
{
print("TutorialPageVC > pageViewController > willTransitionTo")
}
public func pageViewController(_ pageViewController: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers: [UIViewController],
transitionCompleted completed: Bool)
{
print("TutorialPageVC > pageViewController > didFinishAnimating")
if completed
{
if let currentViewController = pageViewController.viewControllers?.first,
let index = pagesArray.firstIndex(of: currentViewController)
{
contentPage = index
print(contentPage)
}
}
}
}
我一直在寻找解决方案的一段时间,发现了这些问题:
PageViewController pass data to child controller
Passing data between UIPageViewController Child views with Swift
PageViewController - Pass variables to child views
how to pass data from UIPageViewController to child ViewController using delegates
我知道这是一个普遍的问题,但是不幸的是,我发现没有一种解决方案对我有用,而且我也看不到为什么和缺少什么。
有什么想法吗?
答案 0 :(得分:0)
替换
return pagesArray[previousIndex]
使用
let item = pagesArray[previousIndex]
item.index = previousIndex
return item
以此类推
class VCInstance:UIViewController {
var index:Int!
}
也
lazy var pagesArray : [UIViewController] =
使用
lazy var pagesArray : [VCInstance] =