gpt4 book ai didi

ios - 对 开始/结束外观转换的不平衡调用

转载 作者:可可西里 更新时间:2023-11-01 06:18:26 25 4
gpt4 key购买 nike

基于Apple documentation我想出了以下方法来在包含 Controller 中的 Controller 之间切换。当有一个 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];
}
}

我是这样调用它的:

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

为了追踪这一点,我记录了外观转换

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

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

这是日志的样子:

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

现在我有点疑惑了。这里有什么问题?

最佳答案

原来 transitionFromViewController:toViewController:duration:options:animations:completion: 也添加了 View 。

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.

这意味着需要相应地调整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];
}
}

关于ios - 对 <UIViewController> 开始/结束外观转换的不平衡调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22676938/

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