简体   繁体   English

对开始/结束外观转换的不平衡调用<UIViewController>

[英]Unbalanced calls to begin/end appearance transitions for <UIViewController>

Based on the Apple documentation I came up with the following method to switch between controllers in a containment controller. 根据Apple文档,我提出了以下方法来在包容控制器​​中切换控制器。 When there is an oldC I am getting Unbalanced calls to begin/end appearance transitions for <...> on the console. 当有一个oldC我在控制台上Unbalanced calls to begin/end appearance transitions for <...>

- (void) showController:(UIViewController*)newC withView:(UIView*)contentView animated:(BOOL)animated
{
    UIViewController *oldC = self.childViewControllers.firstObject;
    if (oldC == newC) {
        return;
    }

    [oldC willMoveToParentViewController:nil];

    [self addChildViewController:newC];
    newC.view.frame = (CGRect){ 0, 0, contentView.frame.size };
    [contentView addSubview:newC.view];

    if (animated && oldC != nil) {
        oldC.view.alpha = 1.0f;
        newC.view.alpha = 0.0f;
        [self transitionFromViewController:oldC toViewController:newC duration:0.25f options:0 animations:^{

            oldC.view.alpha = 0.0f;
            newC.view.alpha = 1.0f;

         } completion:^(BOOL finished) {
            [oldC removeFromParentViewController];
            [newC didMoveToParentViewController:self];
         }];
    } else {
        oldC.view.alpha = 0.0f;
        newC.view.alpha = 1.0f;
        [oldC removeFromParentViewController];
        [newC didMoveToParentViewController:self];
    }
}

This is how I call it: 这就是我所说的:

- (IBAction) buttonSignIn:(id)sender
{
    [self showController:self.signInViewController withView:self.contentView animated:(sender != nil)];
}

- (IBAction) buttonSignUp:(id)sender
{
    [self showController:self.signUpViewController withView:self.contentView animated:(sender != nil)];
}

To track this down I am logging the appearance transitions 为了追踪这一点,我正在记录外观过渡

-(void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated
{
    [super beginAppearanceTransition:isAppearing animated:animated];
    NSLog(@"**begin %@", self);
}

-(void)endAppearanceTransition
{
    [super endAppearanceTransition];
    NSLog(@"**end** %@", self);
}

This is what the log looks like: 这就是日志的样子:

] **begin <SignInViewController: 0x10c769a20>
] **begin <SignUpViewController: 0x10c768770>
] Unbalanced calls to begin/end appearance transitions for <SignUpViewController: 0x10c768770>.
] **end** <SignUpViewController: 0x10c768770>
] **end** <SignInViewController: 0x10c769a20>

Now I am a little puzzled. 现在我有点困惑。 What's the problem here? 这有什么问题?

Turns out transitionFromViewController:toViewController:duration:options:animations:completion: also adds the view. 结果是transitionFromViewController:toViewController:duration:options:animations:completion:还添加了视图。

This method adds the second view controller's view to the view hierarchy and then performs the animations defined in your animations block. 此方法将第二个视图控制器的视图添加到视图层次结构,然后执行动画块中定义的动画。 After the animation completes, it removes the first view controller's view from the view hierarchy. 动画完成后,它将从视图层次结构中删除第一个视图控制器的视图。

Which means the addSubview needs to be adjusted accordingly. 这意味着需要相应地调整addSubview

- (void) showController:(UIViewController*)newC withView:(UIView*)contentView animated:(BOOL)animated
{
    UIViewController *oldC = self.childViewControllers.firstObject;
    if (oldC == newC) {
        return;
    }

    [oldC willMoveToParentViewController:nil];

    [self addChildViewController:newC];
    newC.view.frame = (CGRect){ 0, 0, contentView.frame.size };

    if (animated && oldC != nil) {
        oldC.view.alpha = 1.0f;
        newC.view.alpha = 0.0f;
        [self transitionFromViewController:oldC toViewController:newC duration:0.25f options:0 animations:^{

            oldC.view.alpha = 0.0f;
            newC.view.alpha = 1.0f;

         } completion:^(BOOL finished) {
            [oldC removeFromParentViewController];
            [newC didMoveToParentViewController:self];
         }];
    } else {
        [contentView addSubview:newC.view];
        oldC.view.alpha = 0.0f;
        newC.view.alpha = 1.0f;
        [oldC removeFromParentViewController];
        [newC didMoveToParentViewController:self];
    }
}

old code 旧代码

[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];
[self.view addSubview:toVC.view];
[toVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.mas_equalTo(0);
 }];

 [self transitionFromViewController:fromVC
                      toViewController:toVC duration:0
                               options:UIViewAnimationOptionTransitionNone
                            animations:^{
                            } completion:^(BOOL finished) {
                                [fromVC.view removeFromSuperview];
                                [fromVC removeFromParentViewController];
                                [toVC didMoveToParentViewController:self];
                                self.currentVC = toVC;
                            }];

new code, it's ok 新代码,没关系

[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];

    [self transitionFromViewController:fromVC
                      toViewController:toVC duration:0
                               options:UIViewAnimationOptionTransitionNone
                            animations:^{
                            } completion:^(BOOL finished) {
                                [fromVC.view removeFromSuperview];
                                [fromVC removeFromParentViewController];
                                [toVC didMoveToParentViewController:self];
                                self.currentVC = toVC;
                            }];

我将持续时间设置为0,这意味着animation:NO对于transitionFromViewController调用为animation:NO

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不平衡的调用以开始/结束UIViewController的外观转换 - Unbalanced calls to begin/end appearance transitions for UIViewController 对...的开始/结束外观转换的不平衡调用 - UIViewController包含 - Unbalanced calls to begin/end appearance transitions for…- UIViewController containment 对QLRemotePreviewContentController的开始/结束外观转换的不平衡调用 - Unbalanced calls to begin/end appearance transitions for QLRemotePreviewContentController CKPresentationControllerRootViewController的开始/结束外观转换的不平衡调用 - Unbalanced calls to begin/end appearance transitions for CKPresentationControllerRootViewController 呼叫开始/结束状态转换的不平衡呼叫<UINavigationController: *****> - Unbalanced calls to begin/end appearance transitions for <UINavigationController: *****> 呼叫开始/结束状态转换的不平衡呼叫<ViewController> - Unbalanced calls to begin/end appearance transitions for <ViewController> 开始/结束外观转换的呼叫不平衡 - Unbalanced calls to begin/end appearance transitions Swift Unbalanced 调用开始/结束外观过渡 - Swift Unbalanced calls to begin/end appearance transitions for 对开始/结束外观转换的不平衡调用 <UIViewController: 0x176c0bd0> 在导航控制器中 - Unbalanced calls to begin/end appearance transitions for <UIViewController: 0x176c0bd0> within a Navigation Controller 在tableView中选择单元格时,对UIViewController的开始/结束外观过渡的不平衡调用 - Unbalanced calls to begin/end appearance transitions for UIViewController when selecting cells in tableView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM