Search code examples
iosswiftuiviewcontrollerdelegatesuipageviewcontroller

Delegate Returning Incorrect Instance Number of ViewController


I have a ViewController "LandingPageViewController" which has an embedded PageViewController "pageViewController".

I have created a delegate function to write from "pageViewController" to LandingPageViewControlelr". This works fine and the data is sent to the "LandingPageController" however i cant interact with any of its elements because the delegate is creating a new instance.

enter image description here

When the app first launches it goes to "LandingPageViewController" and if I print(self) in viewDidLoad and viewDidAppear this is the result:

Delegate viewDidLoad...<ParseStarterProject_Swift.LandingPageViewController: 0x7fbf29406e60>
Delegate viewDidAppear...<ParseStarterProject_Swift.LandingPageViewController: 0x7fbf29406e60>

Then once i have scrolled over to the 3rd page and the delegate is called i get the following result of print(self)

Delegate function...<ParseStarterProject_Swift.LandingPageViewController: 0x7fbf2940e3a0>

enter image description here

This is the code from my "LandingPageViewController" that is receiving the data:

protocol GetMyIndexDelegate {
    func getIndex(index: Int)
}

class LandingPageViewController: UIViewController, GetMyIndexDelegate {
 @IBOutlet var createAccountButton: UIButton!

 //delegate functionto get index
func getIndex(index: Int) {
    if index == 3 {
        print("Delegate function...\(self)")

        //print(createAccountButton)

    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    print("Delegate viewDidLoad...\(self)")
}
}

and here is the code from the "pageViewController" class:

class pageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

let myHeight = 667
let myCenter = 333.5

// define the delegate for use
var indexDelegate: GetMyIndexDelegate?


func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if (completed && finished) {
        if let currentVC = pageViewController.viewControllers?.last {
            let index = vcArr.index(of: currentVC)

            if index == 3 {

                // this is the line causing problem
                self.indexDelegate = LandingPageViewController()

                print("PageView Index Defined As...\(self.indexDelegate)")
                indexDelegate?.getIndex(index: index!)

                //goToVC()

            }
        }
    }
}

i believe this is the problem here:

self.indexDelegate = LandingPageViewController()

it is defining incorrect instance?????

As a side clue, i had another delegate method going in the other direction, from a viewController to the first page of the "pageViewController" and the issue there was that i had to correctly define the index of the page within the UIPageViewController with the following code:

self.delegate = self.childViewControllers[0] as! pageViewController

im not sure how this would work in the question above as the LandingPageViewController is the parent? of the pageViewController?


Solution

  • The self.indexDelegate = LandingPageViewController() is incorrect because you are instantiating a new view controller programmatically rather than referring to the one that was already instantiated for you by the storyboard.

    You have an embed segue, which you are using to embed the page view controller within the other view controller's scene. So, you can use prepareForSegue in the parent view controller to set whatever properties you want in the child view controller.

    But if you're simply looking for a reference to the child view controller in the parent, it's childViewControllers.first or childViewControllers[0]. And if the child is looking for a reference to its parent view controller, it's merely parent (or in your case, parent as? GetMyIndexDelegate).