gpt4 book ai didi

ios - 何时调用 addChildViewController

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:46 25 4
gpt4 key购买 nike

我正在尝试使用类似于 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 并推送一个 View Controller 。但是,当我尝试弹出时,我得到了

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

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

最佳答案

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

[self didMoveToParentViewController:toViewController];

成为:

[toViewController didMoveToParentViewController:self];

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

还有一件事我不确定是否正确。在您的 pop 方法中,您的 toViewController 是 _subViewControllers 数组中的最后一个 Controller 。你不想倒数第二个吗?最后一个不应该是你弹出的那个吗?你正在弹出 vc,它是你传递给方法的 Controller ,我不明白。

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

@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 中的所有 Controller (包括自定义容器 Controller )添加导航栏,我能够使用该 Controller 复制后退按钮功能。我向任何将被按下的 Controller 添加了一个条形按钮,并将它们的标题设置为 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 子句中使用的 Controller 设置标题,或者您可以像我在 if 部分中那样使用 navigationItem 标题的条款。 else 子句中的“-3”是必需的,因为在调用 viewWillAppear 时,弹出的 Controller 仍在 childViewControllers 数组中。

关于ios - 何时调用 addChildViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13768458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com