对<UINavigationController:0xa98e050>开始/结束外观转换的不平衡调用

编译我得到的代码

<UINavigationController: 0xa98e050>开始/结束外观转换的不平衡调用”

警告。

这是我的代码

 KVPasscodeViewController *passcodeController = [[KVPasscodeViewController alloc] init]; passcodeController.delegate = self; UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:passcodeController]; [(UIViewController *)self.delegate presentModalViewController:passcodeNavigationController animated:YES]; 

我知道这是一个古老的问题,但为了再次遇到这个问题,我find了这里。

首先,这个问题没有说明新的viewController被调用的地方。
我怀疑这是从-(void)viewDidLoad调用的

将相应的代码移动到-(void)viewDidAppear:并且问题应该消失。

这是因为在-viewDidLoad ,视图已经加载,但尚未呈现,animation和视图尚未完成。

如果您的意图是推窗户,那么在窗户出现并涂漆之后再进行。

如果你发现自己使用计时器来控制系统行为,那么问自己你做错了什么,或者你怎样才能更好地做到这一点。

我发现如果您尝试在前一个事务( animation )进行时推送新的视图控制器,则会发生此问题。

无论如何,我认为,这是presentModalViewController问题,设置animated:NO可能是解决你的问题

 [(UIViewController *)self.delegate presentModalViewController:passcodeNavigationController animated:NO]; 

其他选项是:

NSTimer和以上代码之间的呼叫可能是0.50到1秒。 这也是有用的技巧。 所以你先前的viewController已经完成了它的animation。

当您尝试加载新的viewController,之前包含一个animation完成之前,会出现此警告。 如果您的目的是要这样做,只需将您的代码添加到dispatch_async(dispatch_get_main_queue()块:

 dispatch_async(dispatch_get_main_queue(), ^(void){ [(UIViewController *)self.delegate presentModalViewController:passcodeNavigationController animated:YES]; }); 

警告就会消失。

现代的解决scheme可能是这样的:

 double delayInSeconds = 0.5; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [self.window.rootViewController presentViewController:yourVC animated:YES completion:nil]; }); 

您没有提供太多的上下文,所以我认为这个错误是在启动时发生的,因为您正在提供密码视图控制器。

为了摆脱这个警告,我将应用程序委托注册为导航根视图控制器的委托:

 - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ((UINavigationController *)self.window.rootViewController).delegate = self; return YES; } 

然后我在navigationController:didShowViewController:animated:显示模态视图控制器navigationController:didShowViewController:animated:dispatch_once

 - (void) navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { static dispatch_once_t once; dispatch_once(&once, ^{ KVPasscodeViewController *passcodeController = [[KVPasscodeViewController alloc] init]; passcodeController.delegate = self; UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:passcodeController]; [(UIViewController *)self.delegate presentViewController:passcodeNavigationController animated:YES completion:nil]; }); } 

由于navigationController:didShowViewController:animated:控制器navigationController:didShowViewController:animated:在根视图控制器确实出现之后被调用,所以不平衡调用开始/结束外观转换的警告消失了。