使用容器视图

时间:2012-10-15 09:04:09

标签: objective-c ios ipad uiviewcontroller container-view

这是我的情况。我有一个viewController。在前三个按钮和按钮下方有一个containerview。按下按钮时,容器视图应该更改。

这就是我在故事板中所做的。

  1. 将视图控制器拖到我的故事板中。添加了3个按钮和一个容器视图 这个视图控制器是off class viewController
  2. 将第二个视图控制器拖入其中。并控制从containerView拖动到此视图控制器。并选择了嵌入segue。
  3. 现在在代码中我为控制器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];
    }
    

    对于我的containerController.h,我执行以下操作。

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

    在我的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;
                                }];
    }
    

    但是我一直收到错误"没有已知的选择器切换到第一个实例方法"在我的IBActions中。

    有人能帮助我吗?

    提前致谢。

1 个答案:

答案 0 :(得分:0)

我猜我lastObject正在返回UIViewController并且不知道它是ContainerViewController - 因此不知道switchtoFirst方法。

尝试类似:

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