I've been hitting my head on this for couple of days. My goal is to create an app that has a container view connected to a UIPageViewController such that I can display pages of different data/views. (my existing app w/ UITabBarController w/ 1 single data page/view works)
So far, I've created the containerView and the UIPageController and the different ViewControllers using the guidance from this https://stackoverflow.com/a/26024779/14414215 and going thru a lot of SO pages but unable to get to the result I want.
In the Root VC (Train) I have this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("VCTrain segue ID:(String(describing: segue.identifier))
")
if (segue.identifier == "segueTrainContainerViewPages") {
print("prepare for segueTrainContainerViewPages")
let vcContainerView = segue.destination as! VCTrainContainerViewPages
vcContainerView.myLabel = "HELLO" // This gets Passed
// workoutTitle is obtained from the LIBRARY tab once the user clicks
// on a row in the tableview. It is blank until user selects a workout Title
vcContainerView.myLabel1 = workoutTitle // This remains Blank
}
}
This is the VCTrainContainerViewPages which is basically a copy/paste of https://stackoverflow.com/a/26024779/14414215
import UIKit
class VCTrainContainerViewPages: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pages = [UIViewController]()
var myLabel = String()
var myLabel1 = String()
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "id1")
let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "id2")
let page3: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "id3")
pages.append(page1)
pages.append(page2)
pages.append(page3)
setViewControllers([page1], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)
print("
VCTrainContainerViewPages VDL myLabel :(myLabel)")
print("VCTrainContainerViewPages VDL myLabel1:(myLabel1)
")
}
override func viewDidAppear(_ animated: Bool) {
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController)-> UIViewController? {
let cur = pages.firstIndex(of: viewController)!
var prev = (cur - 1) % pages.count
if prev < 0 {
prev = pages.count - 1
}
return pages[prev]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController)-> UIViewController? {
let cur = pages.firstIndex(of: viewController)!
let nxt = abs((cur + 1) % pages.count)
return pages[nxt]
}
func presentationIndex(for pageViewController: UIPageViewController)-> Int {
return pages.count
}
}
Currently what works:
- The UIPageViewController works (swiping works and all the 3 view controllers moves)
- MyLabel gets passed from MainVC to UIPageViewController (Train Container View Pages)
What I need to work
- workout Title from the Library Tab. This gets passed from the Library Tab (once user selects a workout) to the Train Tab and then gets passed to Train Container View Pages
The prepare for Segue function in Root VC (Train) only gets called 1x during the initial load of the TRAIN page. Subsequent load of this page doesn't call prepareForSegue ever again.
In the working scenario (w/o the container view / UIPageController), I was using ViewDidAppear to receive the data passed from the LIBRARY Tab. Is it possible to use ViewDidAppear to pass the data?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…