Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform a segue programmatically

Tags:

Hi I'm trying to perform a segue programmatically without the Storyboard. Currently I 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's saying that:

has no segue with identifier 'popOutNoteExpanded'.

Not sure how to perform my custom animated transition.

like image 582
ajayb Avatar asked May 05 '17 05:05

ajayb


People also ask

How do you trigger a segue?

First, select a segue in your storyboard, then go to the attributes inspector and give it a name such as “showDetail”. Technically the sender parameter is whatever triggered the segue, but you can put whatever you want in there.

How do you unwind segue programmatically?

You can perform an unwind segue programmatically by 1. creating a manual segue (ctrl-drag from File's Owner to Exit), 2. giving it a name in IB, then 3. doing a normal -performSegueWithIdentifier:sender: in your code.


2 Answers

To trigger a segue programmatically, you have to follow steps:
1. Set it up in Storyboard by dragging your desired segue between two view controllers and set its identifier (for example, in your case is "popOutNoteExpanded") in Attributes Inspector section.
2. Call it programmatically

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

Please check if you set its identifier correctly or not.

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

let cell = tableView.cellForRow(at: indexPath)  

And then you can perform a segue:

performSegue(withIdentifier: "popOutNoteExpanded", sender: cell) 
like image 93
Quang Nguyen Avatar answered Nov 03 '22 02:11

Quang Nguyen


Segues are components of storyboard. If you don't have a storyboard, you can't perform segues. But you can use present view controller like this:

let vc = ViewController() //your view controller self.present(vc, animated: true, completion: nil) 
like image 28
kkakkurt Avatar answered Nov 03 '22 02:11

kkakkurt