Working with a container view

1.1k Views Asked by At

This is my situation. I have a viewController. With at the top three buttons and below the buttons a containerview. When you push a button, the container view should change.

This is what I have done in storyboard.

  1. Dragged a view controller into my storyboard. added 3 buttons and a containerview into it.
    This view controller is off class viewController
  2. Dragged a second viewcontroller into it. And controlled dragged from the containerView to this view controller. and selected embed segue.

Now in code I do the following for controller viewController

-(IBAction)chooseFirstController:(id)sender {
    [self.childViewControllers.lastObject switchToFirst];

}

-(IBAction)chooseSecondController:(id)sender {
    [self.childViewControllers.lastObject switchToSecond];
}
-(IBAction)chooseThirdController:(id)sender {
    [self.childViewControllers.lastObject switchToThird];
}

And for my containerController.h I do the following.

@interface ContainerViewController : ViewController

@property (nonatomic,strong) FirstController *cont1;
@property (nonatomic,strong) SecondController *cont2;
@property (nonatomic,strong) ThirdController *cont3;
@property (nonatomic,strong) ContainerViewController *currentController;

And in my container.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.cont1 = [[FirstController alloc]initWithNibName:@"FirstController" bundle:nil];
    self.cont2 = [[SecondController alloc]initWithNibName:@"SecondController" bundle:nil];
    self.cont3 = [[ThirdController alloc]initWithNibName:@"ThirdViewController" bundle:nil];
    [self addChildViewController:self.cont1];
    self.currentController = self.cont1;
    [self.view addSubview:self.cont1.view];
}

-(void)switchToFirst {
    if (self.currentController != self.cont1) {
        [self addChildViewController:self.cont1];
        [self moveToNewController:self.cont1];
    }
}

-(void)switchToSecond {
    if (self.currentController != self.cont2) {
        [self addChildViewController:self.cont2];
        [self moveToNewController:self.cont2];
    }
}
-(void)switchToThird {
    if (self.currentController != self.cont3) {
        [self addChildViewController:self.cont3];
        [self moveToNewController:self.cont3];
    }
}

-(void)moveToNewController:(id) newController {
    [self.currentController willMoveToParentViewController:nil];
    [self transitionFromViewController:self.currentController toViewController:newController duration:.6 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{}
                            completion:^(BOOL finished) {
                                [self.currentController removeFromParentViewController];
                                [newController didMoveToParentViewController:self];
                                self.currentController = newController;
                            }];
}

But I keep getting error "No known instance method for selector switchtoFirst" in my IBActions.

Can anybody help me?

Thanks in advance.

1

There are 1 best solutions below

0
On

I would guess that lastObject is returning a UIViewController and doesn't know it's a ContainerViewController - hence doesn't know about the switchtoFirst method.

Try something like:

-(IBAction)chooseFirstController:(id)sender {
    UIViewController *vc = self.childViewControllers.lastObject;
    if([vc isKindOfClass:[ContainerViewController class]]) {
        ContainerViewController *containerVC = (ContainerViewController *)vc;
        [containerVC switchtoFirst];
    }
}