iOS - Pass Data Multiple Times From Parent View Into Container View's View

337 views Asked by At

I have a UIPageViewController, and one of its View Controllers has a Container View where its parent View Controller passes data into it so the container can display dynamic data. I currently have the parent view passing data into the view within the container view with this prepareForSegue method:

//In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"CALLED PREPARE FOR SEGUE");
    if ([segue.identifier isEqualToString:@"viewEmbed"]) {
        //pass data
    }
}

However, this prepareForSegue method is only called once (note my NSLog debugging statement), but I need to pass different data to the child view every time the UIPageViewController swipes to the parent view. I also noticed that the viewDidLoad method fires only once in my parent view because the UIPageViewController retains the state of its pages.

Is there a way to re-instantiate (call viewDidLoad again) the parent view every time it is swiped to from the UIPageViewController? I'm thinking that would make the prepareForSegue method fire more than once, which I need it to do. Or am I thinking about this the wrong way and there is a more elegant solution?

Any help or advice would be appreciated. Thanks.

1

There are 1 answers

4
Nils Ziehn On BEST ANSWER

The view did load is only called once as is the segue since your view will only be loaded once. You should do this manually by

  • register for delegate updades on the UIPageViewController
  • impelemt

    - (void)pageViewController:(UIPageViewController * nonnull)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> * nonnull)pendingViewControllers to receive these updates

  • call a method on you child but not the viewDidLoad,viewWillAppear or viewDidAppear since there is code that should not executed multiple times. You should copy you current viewDidLoad code from you child into a new method and call this method from the parent when the delegate method is called