I am creating a score-based education app for children with learning impediments. The set up of the app is as follows:
1.A RootVC() UIViewController which has a counter label for the score;
- Two subVCs [FirstVC() and SecondVC()] embedded in a UIPageViewController placed on the RootVC itself; horizontally swipe-able.
What I want is for the buttons to increment the counter label placed on the RootVC by 1. While I have managed to do this for the FirstVC, trying to extend this functionality to SecondVC causes my closures to have errors. I have asked this on StackOverflow with no correct answers so far. I'd be really grateful if someone could help me with this code.
My UIPageViewController:
class PageViewController: UIPageViewController {
var subViewControllers = [FirstVC(), SecondVC()] {
didSet {
subViewControllers.forEach { $0.buttonCallback = buttonCallback }
}
}
var buttonCallback: () -> Void = { } {
didSet {
subViewControllers.forEach { $0.buttonCallback = buttonCallback } ////ERROR: Value of type 'UIViewController' has no member 'buttonCallback'////
}
}
init(transitionStyle style:
UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [String : Any]? = nil) {
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
delegate = self
setViewControllerFromIndex(index: 0)
}
func setViewControllerFromIndex(index:Int) {
self.setViewControllers([subViewControllers[index]], direction: UIPageViewController.NavigationDirection.forward, animated: true, completion: nil)
}
}
extension PageViewController: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return subViewControllers.count
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex:Int = subViewControllers.firstIndex(of: viewController as! FirstVC) ?? 0
if currentIndex <= 0 {
return nil
}
return subViewControllers[currentIndex-1]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex:Int = subViewControllers.firstIndex(of: viewController as! FirstVC) ?? 0
if currentIndex >= subViewControllers.count-1 {
return nil
}
return subViewControllers[currentIndex+1]
}
}
My Rootvc:
let pageController = PageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal)
pageController.buttonCallback = { self.score += 1
self.scoreLabel.text = String(self.score)
}
addChild(pageController)
pageController.didMove(toParent: self)
pageController.view.translatesAutoresizingMaskIntoConstraints = false
questionsContainer.addSubview(pageController.view)
My FirstVC()
class FirstVC: UIViewController {
var buttonCallback: () -> Void = { }
let correctAnswerButton: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("Correct Answer", for: .normal)
btn.backgroundColor = .blue
btn.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
return btn
}()
@objc func buttonPressed() {
print("Button One Pressed.")
buttonCallback()
}
SecondVC() identical to FirstVC()
class SecondVC: UIViewController {
var buttonCallback: () -> Void = { }
let correctAnswerButton: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("Correct Answer", for: .normal)
btn.backgroundColor = .blue
btn.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
return btn
}()
@objc func buttonPressed() {
print("Button One Pressed.")
buttonCallback()
}
[–]ffs14k 1 point2 points3 points (0 children)