override func viewDidDisappear(_ animated: Bool) {
tableView.visibleCells.forEach {
if let cell = $0 as? PostsWithVideoCustom , let player = cell.player {
player.pause()
cell.player = nil
}
}
}
我在viewDidDisappear
上使用了此代码,通常没问题。
但是,如果我在performSegue
上viewWillAppear
override func viewWillAppear(_ animated: Bool) {
if userDefaults.string(forKey: "goToEvent") != nil {
performSegue(withIdentifier: "postToEvent", sender: self)
}
}
我在第tableView.visibleCells.forEach
行上崩溃了
如何处理此崩溃以及可能导致什么原因?
更新
此崩溃仅在应用被杀死后启动时发生,而从后台唤醒时则不会发生
崩溃
Crashed: com.apple.main-thread
0 APP 0x104521cd4 specialized Posts.viewDidDisappear(_:) (Posts.swift:2127)
1 APP 0x104519864 @objc Posts.viewWillAppear(_:) + 4334082148
2 UIKitCore 0x247561684 -[UIViewController _setViewAppearState:isAnimating:] + 488
3 UIKitCore 0x247562008 -[UIViewController __viewDidDisappear:] + 144
4 UIKitCore 0x2474c31d8 -[UINavigationController viewDidDisappear:] + 232
5 UIKit 0x23b4b5ea8 -[UINavigationControllerAccessibility viewDidDisappear:] + 40
6 UIKitCore 0x247561684 -[UIViewController _setViewAppearState:isAnimating:] + 488
7 UIKitCore 0x247562008 -[UIViewController __viewDidDisappear:] + 144
8 UIKitCore 0x247498890 -[UITabBarController viewDidDisappear:] + 100
9 UIKitCore 0x247561684 -[UIViewController _setViewAppearState:isAnimating:] + 488
10 UIKitCore 0x247562008 -[UIViewController __viewDidDisappear:] + 144
11 UIKitCore 0x247564168 __64-[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:]_block_invoke.1383 + 44
12 UIKitCore 0x247562764 -[UIViewController _executeAfterAppearanceBlock] + 88
13 UIKitCore 0x247b10ef8 _runAfterCACommitDeferredBlocks + 564
14 UIKitCore 0x247aff93c _cleanUpAfterCAFlushAndRunDeferredBlocks + 352
15 UIKitCore 0x247b1e5a8 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 136
16 CoreFoundation 0x21b29ee68 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
17 CoreFoundation 0x21b29e76c __CFRunLoopDoBlocks + 272
18 CoreFoundation 0x21b2997d0 __CFRunLoopRun + 1024
19 CoreFoundation 0x21b2990b0 CFRunLoopRunSpecific + 436
20 GraphicsServices 0x21d49979c GSEventRunModal + 104
21 UIKitCore 0x247b05978 UIApplicationMain + 212
22 APP 0x1044f8504 main (InboxInterests.swift:22)
23 libdyld.dylib 0x21ad5e8e0 start + 4
答案 0 :(得分:3)
我通过添加
解决了这个问题if tableView != nil {
}
这是一个非常简单的修复程序。但是我认为iOS应该首先自己管理。
答案 1 :(得分:0)
在viewWillAppear
之后放置一些延迟,然后按顺序。在这里,该怎么做:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
if userDefaults.string(forKey: "goToEvent") != nil {
performSegue(withIdentifier: "postToEvent", sender: self)
}
}
}
另一件事:在覆盖任何方法时,请不要忘记调用超级方法,否则有100%的错误行为机会。如果您了解方法重载,那么您一定知道这一点。