Работа с контейнерен изглед

Това е моята ситуация. Имам viewController. С горните три бутона и под бутоните изглед на контейнер. Когато натиснете бутон, изгледът на контейнера трябва да се промени.

Това е, което направих в сценария.

  1. Преместих контролер за изглед в моята сценария. добави 3 бутона и изглед на контейнер към него.
    Този контролер за изглед е извън класа viewController
  2. Плъзнете втори контролер за преглед в него. И контролирано плъзгане от 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;

И в моя контейнер.м

- (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;
                            }];
}

Но продължавам да получавам грешка „Няма известен метод на екземпляр за селектор switchtoFirst“ в моите IBActions.

Може ли някой да ми помогне?

Благодаря предварително.


person Steaphann    schedule 15.10.2012    source източник


Отговори (1)


Предполагам, че lastObject връща UIViewController и не знае, че е ContainerViewController - следователно не знае за метода switchtoFirst.

Опитайте нещо като:

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