【前提】
项目中有个需求:点击App的远程推送消息后,跳转到指定页面。具体跳转过程是:点击推送后先跳转页面A,如果满足条件立刻跳转到页面B,如果不满足立刻跳转到页面C。
【问题】
当App在后台运行时,这个跳转没有问题,可以跳转到指定页面B或者C。但是当App没有运行时,会卡在页面A上,并没有跳到B或者C。如果点击返回按钮,会显示一个黑屏,像是崩溃了,实际并没有崩溃。查看log,里面有一句:Unbalanced calls to begin/end appearance transitions。
【分析】
百度了一下,有人说在自定义的TabBarViewController中增加[self.selectedViewController beginAppearanceTransition: YES animated: animated];这样的方法。没有效果。
想起来这种连续push的情况是很有可能发生问题的,因为在画面A的viewDidLoad中进行了网络请求,请求结果回来后直接push到B或者C。
看下stackoverflow上的回答:
“occurs when you try and display a new viewcontroller before the current view controller is finished displaying.”
“Basically you are trying to push two view controllers onto the stack at almost the same time.”
也就是说在当前画面显示完成前试图显示新的画面就出现这个问题。
【方案】
将viewDidLoad中的网络请求和跳转处理拿到viewDidAppear中去解决了问题。