我只有一个简单的 UIAlertController,我通过单击按钮来显示它:
let alert = UIAlertController(title: "", message: NSLocalizedString("Are you sure you want to log out?", comment: ""), preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Sign Out", style: UIAlertActionStyle.default, handler: { (alert: UIAlertAction) in
self.dismiss(animated: true, completion: nil) // CRASH
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
我想通过此对话框操作关闭我的 Controller 。
因此,当我单击“警报”中的“退出”按钮时,应用程序崩溃了。
崩溃日志:
-[UICollectionViewData validateLayoutInRect:]、/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.33.6/UICollectionViewData.m:435 中断言失败 2018-02-23 00:11:17.741531+0300 App[4681:1373962] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UICollectionView 收到索引路径不存在的单元格的布局属性” :{长度= 2,路径= 0 - 0}'
但是!我在这个 Controller 中根本没有 CollectionView。注意:如果我只使用一个简单的 self.dismiss(...) 而没有此警报操作,那么我的 Controller 会像平常一样被解雇。
注 2: 我想要关闭的 Controller 是 SplitViewController,并且我没有任何 CollectionView。
注3:我使用 self.present(splitVC,animated: true) 以简单的方式展示我的 SplitViewController
有什么建议吗?
最佳答案
问题已解决。
此崩溃是由layoutAttributesForElements 引起的。我的缓存数组未清除,当调用layoutAttributesForElements 时出现崩溃。
如果您有自定义 CollectionView 布局,那么您应该始终在 UICollectionViewLayout 的prepare() 中清除缓存。
override func prepare() {
cache = [UICollectionViewLayoutAttributes]()
...
}
这就是问题所在。
关于ios - 在 UIAlertController Action 中关闭 UIViewController 会导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48937377/