Search code examples
iosobjective-ciphoneuicontainerview

Interaction between View Controller in Container View and Parent Controller in Objective C


I only recently started programming for iOS (in Objective C). I am pretty new to most concepts of iOS programming. I've been able to get by with Tutorials and stackoverflow threads, but now I've run into a wall. I am using the Storyboard to build my interface, so right now I have this set-up:

I have a standard "View Controller" which contains a "Container View" and two buttons. The Container View embeds a "Page View Controller".

How do I now use the buttons in the Parent "View Controller" to execute methods in the "Page View Controller"? I have heard of Delegation or working with Selectors, but I really don't understand how either of those would work in code.

Thank you in advance!


Solution

    1. Create a property for page view controller in your View controller's interface declaration.

    @property (nonatomic, assign) UIPageViewController *pageVC;

    1. Give an 'id' to the segue which embeds the page vc within the container, say segueIdentifier.
    2. Implement prepareForSegue method in view controller and access the segue's destination view controller to assign it to the local property
        -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
            if ([segue.identifier isEqualToString:@"segueIdentifier"]) {
                if ([segue.destinationViewController isKindOfClass:[UIPageViewController class]]) {
                    self.pageVC = (UIPageViewController *)segue.destinationViewController;
                }
            }
        }
    

    Once the segue is executed, you can directly access the embedded page view controller form the main view controller.