VC之间切换时.reloadData()问题

时间:2019-05-08 16:24:52

标签: ios swift uicollectionview cocoapods

我尝试使用自定义过渡动画:https://github.com/AladinWay/TransitionButton

它按预期工作,但是由于某种原因,切换到新的VC应用程序后,它停止了。并显示错误"Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"

我已将问题成功定位到第

DispatchQueue.main.async {
                     self.collectionView.reloadData ()
                 }

在没有动画的情况下直接过渡到VC会出现问题。通过重新加载视图来加载和更新信息。

也许是在直接过渡到

 self.present (secondVC, animated: true, completion: nil)

我试图删除self.collectionView.reloadData()行 则不会发生错误。它只是一个黑屏。 但这也是一个问题。

起初,我认为问题出在Pod,但我认为问题在于直接过渡到VC,并且CollectionView只是不加载,而当我尝试升级时,我得到了错误

import TransitionButton
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var searchTextField: UITextField!




    override func viewDidLoad() {
        super.viewDidLoad()


    }

    @IBAction func buttonAction(_ button: TransitionButton) {
        button.startAnimation() // 2: Then start the animation when the user tap the button
        let qualityOfServiceClass = DispatchQoS.QoSClass.background
        let backgroundQueue = DispatchQueue.global(qos: qualityOfServiceClass)
        backgroundQueue.async(execute: {

            sleep(3) // 3: Do your networking task or background work here.

            DispatchQueue.main.async(execute: { () -> Void in
                // 4: Stop the animation, here you have three options for the `animationStyle` property:
                // .expand: useful when the task has been compeletd successfully and you want to expand the button and transit to another view controller in the completion callback
                // .shake: when you want to reflect to the user that the task did not complete successfly
                // .normal
                button.stopAnimation(animationStyle: .expand, completion: {
                    let secondVC = CarInfo()
                    self.present(secondVC, animated: true, completion: nil)
                })
            })
        })
    }
}

import TransitionButton
import UIKit

class CarInfo: CustomTransitionViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var testSegue: UILabel!
    @IBOutlet weak var collectionView: UICollectionView!


    override func viewDidLoad() {
        super.viewDidLoad()




        // create post request


                DispatchQueue.main.async {
                    self.collectionView.reloadData()
                }
            } catch {
                print(error)
            }

        }
        task.resume()
}

我计划在显示完成的页面后在后台使用此动画从API加载数据。 但是现在一切都破了

1 个答案:

答案 0 :(得分:1)

您应该从情节提要中加载该vc,因此替换并设置情节提要标识符,例如new template version

CardID

使用

let secondVC = CarInfo()