ios - perform segue from UIView -


i have viewcontroller , there uiview in it.

this uiview has separate class myview , there many ui elements - 1 of them collectionview.

what want perform segue when 1 of collection elements in myview selected. when try add line

performsegue(withidentifier: "myidintifier", sender: self) 

to collection's view didselectitemat method error

use of unresolved identifier 'performsegue'

and understand because inside class extends uiview , not uiviewcontroller.

so how can perfrom segue in case? , how can prepare segue?

here going evaluate in step step manner.

step - 1

create custom delegate using protocol below snippet guide on custom uiview. protocol must exist out of custom view scope.

protocol celltapped: class {     /// method     func cellgottapped(indexofcell: int)  } 

don't forgot create delegate variable of above class below on custom view

var delegate: celltapped! 

go collection view didselect method below

func collectionview(_ collectionview: uicollectionview, didselectitemat indexpath: indexpath) {         if(delegate != nil) {             self.delegate.cellgottapped(indexofcell: indexpath.item)         }     } 

step - 2

let's come view controller. give celltapped viewcontroller.

class viewcontroller: uiviewcontroller,celltapped {      @iboutlet weak var myview: myuiview! //here custom view outlet     override func viewdidload() {         super.viewdidload()         myview.delegate = self  //assign delegate self     }      // here event while tapped cell. inside can perform performsegue method.     func cellgottapped(indexofcell: int) {         print("tapped cell \(indexofcell)")     } } 

hope you.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -