我正在编写一个包含三个视图的Swift应用程序。我通过在容器中放置一个UIPageViewController并在您从另一个Stack Overflow文章UIPageViewController and storyboard进行滑动时使用该开关来实现此目的。我还希望在底部有点表示您所在的页面。但是,我正在使用的UIPageControl只能放在带容器的视图控制器上,而当前页面的代码位于页面控制器的代码中。这是我的代码:
class ContainerView : UIViewController {
@IBOutlet var myPageController: UIPageControl!
}
class MyPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pages = [UIViewController]()
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "Page1ID")
let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "Page2ID")
let page3: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "Page3ID")
pages.append(page1)
pages.append(page2)
pages.append(page3)
setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
if (currentIndex == 0) {
return pages[currentIndex]
}
let previousIndex = abs((currentIndex - 1) % pages.count)
//myPageControl.currentPage = previousIndex
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
if (currentIndex == 2) {
return pages[currentIndex]
}
let nextIndex = abs((currentIndex + 1) % pages.count)
//myPageControl.currentPage = nextIndex
return pages[nextIndex]
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return pages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
如何从容器中的子节点(MyPageViewController)访问父视图控制器(ContainerView)?我是Swift / Objective-C的新手,但已经用其他语言编程(C ++,Python)。如果您需要故事板,请询问。