0

I am not fluent in English. Sorry...

I have a modalView with collectionView. I use didselect function. If I select a cell, the modalView will dismiss, and want to move originView -> newView.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    dismiss(animated: true, completion: nil)
    performSegue(withIdentifier: "NewView", sender: nil)
}

It dose not work. Just dismiss not performSegue. How should I do

Arasuvel
  • 2,971
  • 1
  • 25
  • 40
JJHA
  • 76
  • 7
  • 1
    Your design makes no sense. You dismiss (throw away) the view which is supposed to be the source of the segue. You should connect the segue from the **parent** view of the modal view and use a callback in the `completion` parameter. – vadian Apr 08 '22 at 10:06

1 Answers1

0

Create a segue between your OriginViewController and NewViewController then perform the segue after dismissing the CollectionViewController.

Segue: [OriginViewController] --NewViewSegue--> [NewViewController]

class OriginViewController: UIViewController {

}

class NewViewController: UIViewController {
    
}

class CollectionViewController: UIViewController {

     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            
          // presentingViewController is your OriginViewController
          let presentingVC = self.presentingViewController
          // change animated parameter to false if you need to skip the dismiss animation
          self.dismiss(animated: true) {
              presentingVC?.performSegue(withIdentifier: "NewViewSegue", sender: nil)
          }
     }
}

Sample

enter image description here

Another method is you can use a delegate on dismiss completion. How to use a delegate.

Sreekuttan
  • 1,579
  • 13
  • 19