marisbest2
marisbest2

Reputation: 1356

Load a specific subview into a container with superview's button

I'm building an app that should be able to load different subviews into a container in the main view like so: storyboard

When the light green view loads, the yellow view should be displayed inside the container. (Ignore the buttons at the top of the yellow view. In the real thing, they shouldnt be there at all)

The app will store a value remembering which view should be loaded if the user presses either "play local" or "play youtube". If the user presses "play local" then one of the orange/blue/light blue should be loaded, and if they press "play youtube" the dark/brown/purple view should be loaded.

If the clear button is pressed, the container should display the yellow view again.

How can I get the buttons in the green controller to reference the views to be displayed inside the Container?

Upvotes: 0

Views: 415

Answers (2)

rdelmar
rdelmar

Reputation: 104092

You should implement prepareForSegue in the green controller. That will be called right after it is instantiated because of its embed segue to the navigation controller.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSURLRequest *)sender {
    UINavigationController *nav = segue.destinationViewController;
    self.yellow = nav.topViewController; // yellow is a property pointing to your yellow view controller;
}

Now, in your button methods, you can just call whichever segue you want based on your logic,

[self.yellow preformSegueWithIdentifier:@"Orange" sender:self];

Upvotes: 1

JIthin
JIthin

Reputation: 1453

Write the following code in the function which will be called on the button press.

   UIStoryboard* storyboard = [self storyboard];
        PlayLocalViewController *yourView = [storyboard instantiateViewControllerWithIdentifier:@"PlayeLocalControllerID"];                                                        //yourViewis the ViewController object
        yourView.view.frame = self.yourContainerView.bounds;
                               //youContainerView is the containerView property.
        [self.yourContainerView addSubview:yourView.view];
        [self addChildViewController:yourView];
        [yourView didMoveToParentViewController:self];

Upvotes: 0

Related Questions