2

我正在尝试使用类似于 UINavigationController 的东西,以便可以自定义动画。首先,我只是使用 Apple 库存动画。这是我的容器视图控制器:

- (void)loadView {
    // Set up content view
    CGRect frame = [[UIScreen mainScreen] bounds];
    _containerView = [[UIView alloc] initWithFrame:frame];
    _containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.view = _containerView;

}

- (id)initWithInitialViewController:(UIViewController *)vc {
    self = [super init];
    if (self) {
        _currentViewController = vc;

        [self addChildViewController:_currentViewController];
        [self.view addSubview:_currentViewController.view];
        [self didMoveToParentViewController:self];

        _subViewControllers = [[NSMutableArray alloc] initWithObjects:_currentViewController, nil];
    }
    return self;
}



- (void)pushChildViewController:(UIViewController *)vc animation:(UIViewAnimationOptions)animation {
    vc.view.frame = _containerView.frame;
    [self addChildViewController:vc];

    [self transitionFromViewController:_currentViewController toViewController:vc duration:0.3 options:animation animations:^{

    }completion:^(BOOL finished) {
        [self.view addSubview:vc.view];
        [vc didMoveToParentViewController:self];
        [self.subViewControllers addObject:vc];
    }];
}

- (void)popChildViewController:(UIViewController *)vc WithAnimation:(UIViewAnimationOptions)animation {
    // Check that there is a view controller to pop to
    if ([self.subViewControllers count] <= 0) {
        return;
    }

    NSInteger idx = [self.subViewControllers count] - 1;
    UIViewController *toViewController = [_subViewControllers objectAtIndex:idx];
    [vc willMoveToParentViewController:nil];

    [self transitionFromViewController:vc toViewController:toViewController duration:0.3 options:animation animations:^{

    }completion:^(BOOL finished) {
        [vc.view removeFromSuperview];
        [vc removeFromParentViewController];
        [self didMoveToParentViewController:toViewController];
        [self.subViewControllers removeObjectAtIndex:idx];
    }];
}

我有这个 ContainerViewcontroller 作为窗口的 rootViewController。我可以添加我的初始 viewController 并推送一个视图控制器。当我尝试弹出时,我得到

ContainerViewController[65240:c07] Unbalanced calls to begin/end appearance transitions for <SecondViewController: 0x8072130>.

我想知道我做错了什么。我想我的 initialViewController 仍然在 secondViewController 下面。有什么想法吗?谢谢!

4

3 回答 3

4

我不知道这是否是导致您的问题的原因,但不应该这样:

[self didMoveToParentViewController:toViewController];

是:

[toViewController didMoveToParentViewController:self];

另外,我不确定你在用 subViewControllers 数组做什么。它似乎是已经是 UIViewController 属性的 childViewControllers 数组的副本。

我不确定的另一件事是否正确。在您的 pop 方法中,您的 toViewController 是 _subViewControllers 数组中的最后一个控制器。你不希望它是倒数第二个吗?最后一个不应该是你要弹出的那个吗?您正在弹出 vc,这是您传递给方法的控制器,我不明白。

这就是我制作类似控制器的导航的方式。在其包含行为中,它就像一个导航控制器,但没有导航栏,并允许不同的过渡动画:

@implementation ViewController

-(id)initWithRootViewController:(UIViewController *) rootVC {
    if (self = [super init]) {
        [self addChildViewController:rootVC];
        rootVC.view.frame = self.view.bounds;
        [self.view addSubview:rootVC.view];
    }
    return self;
}


-(void)pushViewController:(UIViewController *) vc animation:(UIViewAnimationOptions)animation {
    vc.view.frame = self.view.bounds;
    [self addChildViewController:vc];
    [self transitionFromViewController:self.childViewControllers[self.childViewControllers.count -2] toViewController:vc duration:1 options:animation animations:nil
            completion:^(BOOL finished) {
            [vc didMoveToParentViewController:self];
            NSLog(@"%@",self.childViewControllers);
    }];
}

-(void)popViewControllerAnimation:(UIViewAnimationOptions)animation {
    [self transitionFromViewController:self.childViewControllers.lastObject toViewController:self.childViewControllers[self.childViewControllers.count -2] duration:1 options:animation animations:nil
                            completion:^(BOOL finished) {
                                [self.childViewControllers.lastObject removeFromParentViewController];
                                NSLog(@"%@",self.childViewControllers);
                            }];
}

-(void)popToRootControllerAnimation:(UIViewAnimationOptions)animation {
    [self transitionFromViewController:self.childViewControllers.lastObject toViewController:self.childViewControllers[0] duration:1 options:animation animations:nil
                            completion:^(BOOL finished) {
                                for (int i = self.childViewControllers.count -1; i>0; i--) {
                                    [self.childViewControllers[i] removeFromParentViewController];
                                }
                                NSLog(@"%@",self.childViewControllers);
                            }];
}

编辑后:通过向 IB 中的所有控制器(包括自定义容器控制器)添加导航栏,我能够使用此控制器复制后退按钮功能。我为任何将被推送的控制器添加了一个条形按钮,并将它们的标题设置为 nil(如果我将标题保留为“项目”,我会遇到一些故障)。删除该标题会使按钮消失(在 IB 中),但您仍然可以在场景列表中与它建立连接。我向它添加了一个 IBOutlet,并添加了这段代码来获得我想要的功能:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.isMovingToParentViewController) {
        self.backButton.title = [self.parentViewController.childViewControllers[self.parentViewController.childViewControllers.count -2] navigationItem].title;
    }else{
        self.backButton.title = [self.parentViewController.childViewControllers[self.parentViewController.childViewControllers.count -3] title];
    }
}

我已经展示了访问标题的两种不同方法——在 IB 中,您可以为我在 else 子句中使用的控制器设置标题,或者您可以像在子句的 if 部分中那样使用 navigationItem 标题. else 子句中的“-3”是必要的,因为在调用 viewWillAppear 时,正在弹出的控制器仍在 childViewControllers 数组中。

于 2012-12-07T18:21:12.663 回答
4

addChildViewController应该首先调用

对于添加/删除,您可以参考这个伟大的类别而无需担心何时调用它:

UIViewController + 容器

- (void)containerAddChildViewController:(UIViewController *)childViewController {

    [self addChildViewController:childViewController];
    [self.view addSubview:childViewController.view];
    [childViewController didMoveToParentViewController:self];

}

- (void)containerRemoveChildViewController:(UIViewController *)childViewController {

    [childViewController willMoveToParentViewController:nil];
    [childViewController.view removeFromSuperview];
    [childViewController removeFromParentViewController];

}
于 2013-04-20T21:22:44.053 回答
3

除了 rdelmar 的回答之外,您不应该调用 addView/removeFromSuperviewtransitionFromViewController为您执行此操作,来自文档:

此方法将第二个视图控制器的视图添加到视图层次结构中,然后执行动画块中定义的动画。动画完成后,它会从视图层次结构中移除第一个视图控制器的视图。

于 2012-12-07T18:39:38.493 回答