[英]Dismissing UIViewController in UIAlertController Action causes a crash
我只有一個簡單的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)
我想通過此對話框操作關閉控制器。
因此,當我單擊“警報”中的“注銷”按鈕時,我的應用程序崩潰了。
崩潰日志:
-[UICollectionViewData validateLayoutInRect:],/ BuildRoot / Library / Caches / com.apple.xbs / Sources / UIKit_Sim / UIKit-3698.33.6 / UICollectionViewData.m中的聲明失敗:m:435 2018-02-23 00:11:17.741531 + 0300 App [4681:1373962] *由於未捕獲的異常“ NSInternalInconsistencyException”而終止應用程序, 原因:“ UICollectionView接收到索引路徑不存在的單元格的布局屬性:{長度= 2,路徑= 0-0}”
但! 我在此控制器中根本沒有CollectionView。
注意:如果我僅使用一個簡單的self.dismiss(...)而不執行此警報操作,那么我的控制器將被正常關閉。
注意2:我要關閉的控制器是SplitViewController,我沒有任何CollectionViews。
注意3:我使用self.present(splitVC,animation:true)以簡單的方式展示我的SplitViewController
有什么建議么?
問題解決了。
此崩潰是由layoutAttributesForElements引起的。 我的緩存數組未清除,當layoutAttributesForElements被稱為崩潰時出現。
如果您具有自定義CollectionView布局,則應始終清除UICollectionViewLayout的prepare()中的緩存。
override func prepare() {
cache = [UICollectionViewLayoutAttributes]()
...
}
這就是問題所在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.