我正在尝试使用类似于UINavigationController的东西,以便我可以自定义动画。首先,我只是使用Apple股票动画。这是我的containerViewController:
- (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下面。有什么想法吗?谢谢!
答案 0 :(得分:4)
我不知道这是不是导致你的问题,但不应该这样:
[self didMoveToParentViewController:toViewController];
是:
[toViewController didMoveToParentViewController:self];
另外,我不确定你在使用subViewControllers数组做什么。它似乎是childViewControllers数组的重复,它已经是UIViewController的属性。
另一件事我不确定是对的。在你的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(如果我将标题保留为“item”,我会遇到一些故障)。删除该标题会使按钮消失(在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数组中。
答案 1 :(得分:4)
addChildViewController
对于添加/删除,您可以参考这个伟大的类别,不用担心何时调用它:
- (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];
}
答案 2 :(得分:3)
除了rdelmar的答案,你不应该从文档中调用addView / removeFromSuperview transitionFromViewController
为你做这个:
此方法将第二个视图控制器的视图添加到视图中 层次结构,然后执行动画中定义的动画 块。动画完成后,它会删除第一个视图 视图层次结构中的控制器视图。