最近发现了在iOS7中使用UINavigationController的一个坑,看见stackOverFlow中,也有人在问,刚好也解决了问题,今天就在这儿共享下解决方案吧!
先来看看问题吧,代码如下:(来自stackOverFlow)
I am using following line of code:
[self.navigationController popViewControllerAnimated:YES];
But it is not behaving in ios 7 as it doing in ios 6.Some times it does not pop controller while we are pressing back button 2- 3 times in succession.
Resulting in abrupt behaviour in navigation bar and deallocating a controller but showing the same on ui .
So when we press anything on that controller it results to a crash since controller is already deallocated.
大体意思就是,在iOS 7 中使用popViewControllerAnimated,这个方法,并不像在iOS 6中那么好使(在iOS8 以上也挺好使),就造成了不能正常pop出来的问题,具体愿意,我觉得下边这个人回答的挺好:
I'm happy that I am not the only one this has happened to. iOS 7+ bug (just got it in iOS 8, too). The view is popped from the navigation stack, but not animated from the UI. Then it gets stuck there, since it's no longer a part of the stack!
大体意思:其实改pop出来的页面其实已经从navigation stack中pop出去了,但是UI的动画却没有机结束,然后你又从已经不在navigation stack中的view,继续动画,然后你就会发现卡在那里了,继而程序崩溃!
既然已经找到了问题,那么我开始的思路是,让不要动画,也就是:
[self.navigationController popViewControllerAnimated:NO];
然并卵~
之后想到,既然他的原因是view已经pop出去了,才进行UI的animate,那么我们何不延迟几秒钟,等他view的pop动作结束,然后再进行UI的动画
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.navigationController popViewControllerAnimated:YES];
});
这样就解决了问题!
大家在适配iOS 7的时候,可以借鉴下!