这是我的情况。我有一个viewController。在前三个按钮和按钮下方有一个containerview。按下按钮时,容器视图应该更改。
这就是我在故事板中所做的。
现在在代码中我为控制器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中。
有人能帮助我吗?
提前致谢。
答案 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];
}
}