0

I have a UIViewController VC opened modally.

This viewcontroller, built with a storyboard, contains a navigation bar and a container view.

The container view embeds a UIPageViewController.

The UIPageViewController has its own class.

From this class I need to access to a member of the parent UIViewController (OR, in my case it's the same, pass a value to the child view controller).

How to do it?

I tried to print out the self.parent and self.presentingViewController but it turns out their values are both nil.

I tried another thing too, but it's not performing very well: in UIViewController I refer to the child view controller this way:

var pVC = self.childViewControllers[0] as! PageViewController

this way I can pass a value to the pageViewController:

pVC.myVariable = "Blue"

but I can read it only in viewDidAppear, not in viewDidLoad (and it's slow).

I even tried to do a similar thing in the context of a didSet: it works way better but I don't know if this is very othodox.

1 Answers1

-1

Do not set values or variables in UIViewController class. Make an Initializer of PageViewController and pass the values from another you want to change. Also, make some convenience initializers according to your need. This is the best practice. Do stuff of any class in the class itself.

Can you paste code? so that i can give exact solution what you want achieve.

  • There's practically no code, I made about anything with a storyboard, as explained above –  Feb 15 '18 at 06:52
  • I suppose the PageViewController is initialized when you embed it with a storyboard, so, if I understand well what you say, I can't add a custom initializer to its class if I can reference it from the "embedding" viewController –  Feb 15 '18 at 07:00
  • https://stackoverflow.com/questions/35315404/custom-init-for-uiviewcontroller-in-swift-with-interface-setup-in-storyboard –  Feb 15 '18 at 08:40