1

I am building an app using container views. I have been browsing the internet to find examples on how to use it properly but, unfortunately, I found very few examples and, so far, none of them use storyboards the way I intend to do. Here is a picture of my storyboard: app scheme

The code I wrote is this: (FirstWinViewController.m)

#import "FirstWinViewController.h"
#import "ContainerClassViewController.h"

@interface FirstWinViewController ()

@end

@implementation FirstWinViewController

- (IBAction)clickOne:(id)sender {

    ContainerClassViewController *viewContained = [[self.childViewControllers[0] viewControllers] objectAtIndex:0];

    [viewContained gotoSegue:1];

}
- (IBAction)clickTwo:(id)sender {

    ContainerClassViewController *viewContained = [[self.childViewControllers[0] viewControllers] objectAtIndex:0];

    [viewContained gotoSegue:2];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

(ContainerClassViewController.m)

#import "ContainerClassViewController.h"

@interface ContainerClassViewController ()

@end

@implementation ContainerClassViewController

-(void)gotoSegue:(int)umOuDois {

    switch (umOuDois) {
        case 1:

            [self.navigationController popToRootViewControllerAnimated:NO];

            [self performSegueWithIdentifier:@"seguePush1" sender:nil];
            break;

        case 2:

            [self.navigationController popToRootViewControllerAnimated:NO];

            [self performSegueWithIdentifier:@"seguePush2" sender:nil];
            break;

        default:
            break;
    }

}

It is working exactly the way I want. My questions are:

. Is it right according to Apple's rules?

. Is this approach using more memory, leaving trash or using more resources than the examples that create Container views by code and use AppDelegate to manage them?

Thanks in advance.

user1141890
  • 79
  • 1
  • 7
  • it's **fully explained in great detail here** ... http://stackoverflow.com/a/23403979/294884 Scroll down to the section "Identifier...". It's that easy. – Fattie Jun 21 '16 at 14:44

1 Answers1

2

I'm confused about both your question and @JoeBlow's answer.

Container views do magic for you starting with iOS 6. You drag a container view onto your form in IB, and then control-drag from the container view to another VC. IB offers to create an embed segue for you. You give it an identifier, and then the ebed segue causes the child view controller to be loaded and installed as a child at the time your parent view controller is loaded.

Your parent's prepareForSegue method fires at the time the child is loaded, and that gives you an opportunity to save a pointer to the child, install the parent as a delegate of the child, or whatever other setup you need to do.

I have a sample project on github that demonstrates this using 2 container views, each of which embed table view controllers. The table view controllers and their parents communicate back and forth using simple protocols I defined.

You can see the project at this link: https://github.com/DuncanMC/test

Duncan C
  • 115,063
  • 19
  • 151
  • 241
  • Duncan C, thank you for your answer. The way I did to get a pointer to my viewcontroller inside the container view really worked, but the segue way you pointed worked as well and seems to be a better practice. – user1141890 May 25 '14 at 19:44
  • Magic? Its pretty easy to implement in code which might show you inside the box. – Pétur Ingi Egilsson May 25 '14 at 21:53
  • @Pétur Ok, magic is an overstatement. There is quite a bit of housekeeping you have to do in order to do this manually and get everything to work correctly. – Duncan C May 26 '14 at 00:09
  • As Duncan says above: **"I don't know of a way to set up a link to the child in IB instead of in code"** unfortunately there's still no way to do **that part** in the storyboard. – Fattie Jun 04 '14 at 14:30