iosswiftdelegatesuipageviewcontrollerviewdidload

How to automatically update a label on a viewcontroller from another viewcontroller while using a UIpageviewcontroller to navigate pages


I am currently using a UIPageviewcontroller to navigate between my 3 main viewcontrollers. I have a global variable that I set on the first page using a stepper. The value of that global variable is then used as the value for a label on the second page.

This works, but it requires a button on the 2nd page to update the label. The exception being when I first run the app and change the stepper on the first page before swiping to the second page - as I have the label being updated in the "override func viewdidload()"

I dont want to use a button to update my label on page 2. I would like to be able swipe to page 1, change the stepper, then when I swipe back to page 2, the label on page 2 is automatically updated. (repeatedly)

How do I do this?

Is there a way to kill view controllers within the UIpageviewcontroller? or is there a way to get the "override fun viewdidload()" function to run every time I swipe to a page?


Solution

  • The simple way to do this would be to update the label on page 2 in it's viewWillAppear method. That will get called each time you go back to your page 2 view controller.

    Globals are not a great way to set up your data handling however.

    You'd be better off making page 2's view controller the delegate on the first view controller. When the users changes the stepper on the first view controller, it could call a "valueChanged()` method on it's delegate.

    You should read up on the delegate design pattern to figure out how to do this. It's VERY well documented, and is one of the most common patterns you will find in iOS development.