0

I have a UIPageViewController with 4 View Controllers in it. In the second VC, the user enters some data. In the last VC, there is a button that the user clicks to insert these data into DB. I want to pass the data from the second VC to the fourth one. All my search results show segues, but the VCs are not directly connected.

So I thought of using delegates, but it is not working for me.

Code:

UIPageViewController:

import UIKit

class PageViewController: UIPageViewController {

var pageControl = UIPageControl()

fileprivate lazy var pages: [UIViewController] = {
    return [
        self.getViewController(withIdentifier: "locationStoryBoard"),
        self.getViewController(withIdentifier: "notificationStoryboard"),
        self.getViewController(withIdentifier: "weightStoryboard"),
        self.getViewController(withIdentifier: "bloodTypeStoryboard"),
        self.getViewController(withIdentifier: "ageStoryboard"),
        self.getViewController(withIdentifier: "genderStoryboard"),
        self.getViewController(withIdentifier: "mobileNumberStoryboard"),
        self.getViewController(withIdentifier: "preventionStoryboard")
    ]
}()

fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController
{
    return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: identifier)
}

override func viewDidLoad()
{
    super.viewDidLoad()
    self.dataSource = self
    self.delegate   = self
    
    if let firstVC = pages.first
    {
        setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
    }
    configurePageControl()
}

//Page Control (3 dots)
func configurePageControl() {
    pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
    self.pageControl.numberOfPages = pages.count
    self.pageControl.currentPage = 0
    self.pageControl.alpha = 0.5
    self.pageControl.tintColor = UIColor.black
    self.pageControl.pageIndicatorTintColor = UIColor.white
    self.pageControl.currentPageIndicatorTintColor = #colorLiteral(red: 0.986409843, green: 0.4042935669, blue: 0.4366002679, alpha: 1)
    self.view.addSubview(pageControl)
}

}

//MARK: - UIPageViewControllerDataSource methods
extension PageViewController: UIPageViewControllerDataSource
{
    func pageViewController(_ pageViewController: UIPageViewController,
                            viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let currentIndex = pages.firstIndex(of: viewController)!
        let previousIndex = currentIndex - 1
        return (previousIndex == -1) ? nil : pages[previousIndex]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController,
                            viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let currentIndex = pages.firstIndex(of: viewController)!
        let nextIndex = currentIndex + 1
        return (nextIndex == pages.count) ? nil : pages[nextIndex]
    }
}

    //MARK: - UIPageViewControllerDelegate methods
    extension PageViewController: UIPageViewControllerDelegate {
        func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
            let pageContentViewController = pageViewController.viewControllers![0]
            self.pageControl.currentPage = pages.firstIndex(of: pageContentViewController)!
        }
    }

First VC

     protocol NameAndMobileDelegates {
             func passName(nameToBePassed: String)
        }
        
    class MobileNumberViewController: UIViewController, UITextFieldDelegate {
        
        var delegate: NameAndMobileDelegates?

    ...



func textFieldDidEndEditing(_ textField: UITextField) {
            switch textField.tag {
            case 1:
                name = textField.text!
                self.delegate?.passName(nameToBePassed: name)
                print(name)
            case 2:
                mobileNumber = textField.text!
                print(mobileNumber)
            default: break
            }
    }

In the 4th VC:

// MARK: - Name and Number Delegate Methods
extension DiseasesViewController: NameAndMobileDelegates {
    func passName(nameToBePassed: String) {
        print("protocol worked")
    }

Thank you in advance

mohamad
  • 39
  • 7
  • Does this answer your question? [Passing data between view controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – El Tomato Apr 20 '21 at 21:58
  • There is no difference between your question and other hundreds of similar samo-samo topics. – El Tomato Apr 20 '21 at 21:59
  • @ElTomato can you please tell me how are they the same? You just linked me how to pass data between two connected VCs, I have done it 100 times. My VCs are not connected with segues now. They are connected by UIPageViewControllers. I tried delegates and protocols which I shared the code above, asking for help because its not working. – mohamad Apr 20 '21 at 22:03
  • If you think your topic is different, you are certainly free to use the search function and look for similar ones for yourself. – El Tomato Apr 20 '21 at 22:06
  • @ElTomato I did and did not find solution, Hence, asking the question. Thank you – mohamad Apr 20 '21 at 22:07

1 Answers1

0

Do

if let fourVC = pages[3] as? FourthVC {
   fourVC.delegate = pages.first as! FirstVC
 }

then

class FourthVC:UIViewController {
   weak var delegate:FirstVC?
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • if let fourVC = pages[3] as? FourthVC { fourVC.delegate = self } Should I place that in PageViewController class? `Cannot assign value of type 'PageViewController' to type 'NameAndMobileDelegates?'` – mohamad Apr 20 '21 at 21:17
  • Cannot assign value of type 'PageViewController' to type 'MobileNumberViewController' – mohamad Apr 20 '21 at 21:51
  • its working now thank you very much, but it is bugged. Meaning when I finish editing and swipe, the protocol does not work, however if i go back and press enter or tap the textfield and then swipe, the code works. Thoughts? – mohamad Apr 20 '21 at 22:49