Execute programmatically

Hi, I am trying to execute segue programmatically without a storyboard. I currently have this as my code:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "popOutNoteExpanded" { let vc = segue.destination as! ExpandedCellViewController let cell = sender as! AnnotatedPhotoCell sourceCell = cell vc.picture = cell.imageView.image print("button pressed") } } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { performSegue(withIdentifier: "popOutNoteExpanded", sender: indexPath) } 

When I click on the collection view cell, it says that:

does not matter with the id 'popOutNoteExpanded'.

I don’t know how to make my animated transition.

+7
swift transition uicollectionviewcell
source share
2 answers

Segues are components of storyboard. Unless you have a storyboard, you cannot perform segues. But you can use the current view controller as follows:

 let vc = ViewController() //your view controller self.present(vc, animated: true, completion: nil) 
+7
source share

To program segue programmatically, you need to do the following:
1. Configure it in Storyboard with , by dragging the desired segment between the two view controllers and set its identifier (for example, in your case it is “popOutNoteExpanded”) in the “Attribute inspector” section.
2. Name it programmatically

 performSegue(withIdentifier: "popOutNoteExpanded", sender: cell) 

Verify that your ID is set correctly.

Also, in the code above, you put the wrong sender . In your prepare () method, you use the sender as a UITableViewCell, but you call the performSegue () function with the sender as IndexPath.
You need a cell by calling:

 let cell = tableView.cellForRow(at: indexPath) 

And then you can execute segue:

 performSegue(withIdentifier: "popOutNoteExpanded", sender: cell) 
+15
source share

All Articles