0

这是我的情况。我有一个视图控制器。在顶部三个按钮和按钮下方是一个容器视图。当您按下按钮时,容器视图应该会发生变化。

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

  1. 将视图控制器拖到我的故事板中。在其中添加了 3 个按钮和一个容器视图。
    此视图控制器已关闭类 viewController
  2. 将第二个视图控制器拖入其中。并控制从 containerView 拖到这个视图控制器。并选择嵌入segue。

现在在代码中,我为控制器 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 中不断收到错误“没有选择器 switchtoFirst 的已知实例方法”。

有谁能够帮我?

提前致谢。

4

1 回答 1

0

我猜那lastObject是返回 aUIViewController并且不知道它是 a ContainerViewController- 因此不知道该switchtoFirst方法。

尝试类似:

-(IBAction)chooseFirstController:(id)sender {
    UIViewController *vc = self.childViewControllers.lastObject;
    if([vc isKindOfClass:[ContainerViewController class]]) {
        ContainerViewController *containerVC = (ContainerViewController *)vc;
        [containerVC switchtoFirst];
    }
}
于 2013-03-10T21:49:18.460 回答