Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

ios - Passing data from CollectionView to DetailVC in Swift 4

My CollectionView should pass a class model to DetailViewController, but when I tap on a cell I get the nil error.

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

The CollectionViewController is embedded programmatically on a TabBarController.

Collection View


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return soundArray.count
    }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SoundCell", for: indexPath) as? SoundCell {
    let SoundClass = soundArray[indexPath.row]
    cell.updateUI(SoundClass: SoundClass)
    return cell

    } else {
    return UICollectionViewCell()
    }
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    self.performSegue(withIdentifier: "seguetosound", sender: self)

  }

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "seguetosound" {
    if let detailVC = segue.destination as? DetailSecondVC
    let sound = sender as? SoundClass { 
    detailVC.SoundClass = sound 

    }
 }

Detail View Controller

class DetailSecondVC: UIViewController, UIWebViewDelegate {

private var _SoundClass: SoundClass!

var SoundClass: SoundClass {
        get {
            return _SoundClass
        } set {
            _SoundClass = newValue
        }
    }

Do you know what I am missing here? I tested the segue with a simple white screen and it works but when I try to pass the data, it fails.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The correct approach is this. First, figure out how you want to trigger the segue. One option is, in didSelect, trigger the segue in code:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "seguetosound", sender: self)
}

But even better, just delete didSelectItemAt completely and have the segue in the storyboard come from the cell. That way the segue is triggered automatically when the user taps the cell.

Then, in prepare, find out what index path was selected, and pull out the data from the model and pass it to the destination view controller (this might not compile, because your variable names are so atrocious that I can't read your code, but it is the correct approach generally):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "DetailSecondVC" {
        if let detailVC = segue.destination as? DetailSecondVC {
            if let paths = collectionView?.indexPathsForSelectedItems {
                let row = paths[0].row
                detailVC.SoundClass = SoundClasss[row]
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...