Reputation: 376
I'm trying to change the current ViewController of my UIPageViewController using a NEXT button. So I'm calling the function that's in my containterViewController in my mainViewController using a delegate. But it doesn't execute the setViewControllers line.
Here you can see my code:
This is the method that I call using my delegate:
func forwardPage() {
print("Start")
currentPage += 1
print(vcs[currentPage])
self.setViewControllers([vcs[currentPage]], direction: .forward, animated: true) { (true) in
print("Done")
}
}
Here's my delegate:
protocol WalkthroughViewControllerDelegate {
func forwardPage()
func currentIndex() -> Int
}
And here's the function that is connected to my NEXT button:
@IBAction func nextButtonTapped(_ sender: Any) {
if let index = delegate?.currentIndex() {
print("Here... \(index)")
switch index {
case 0...1:
print("Forwarding...")
delegate?.forwardPage()
case 2:
dismiss(animated: true, completion: nil)
default: break
}
}
updateUI()
}
Everything but the "Done" gets printed
I would really appreciate your help
I've been struggling because of this for quite some time now
Thank you very much :)
EDIT: Maybe this happens because that UIPageViewController is inside a containerView. But I'm not sure
SECOND EDIT: I've created a git-hub repository just for this issue. Here's the link: https://github.com/LennartPhil/App-Popup-Screen. I hope you can understand that I won't show you all of my files.
Upvotes: 5
Views: 2914
Reputation: 77423
OK - the problem is that your code in viewDidLoad()
:
let storyboard = UIStoryboard(name: "ExtraViewControllers", bundle: nil)
let walkthroughPageVC = storyboard.instantiateViewController(withIdentifier: "WalkthroughPageVC") as! WalkthroughPageViewController
delegate = walkthroughPageVC
is creating a NEW instance of WalkthroughPageViewController
, which goes out of scope as soon as viewDidLoad()
exits. So it no longer exists.
What you need to do instead is get a reference to it in prepare(for segue:...)
, and set it as your delegate there:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? WalkthroughPageViewController {
self.delegate = vc
}
}
I forked your GitHub repo and added the files into a project, so you can see it run: https://github.com/DonMag/App-Popup-Screen
Upvotes: 2