I know how to pass data using segues from prepareForSegue function, but the thing i have a TableViewCell from which there are two possible segues to two dif
You should be using the didSelectRowAtIndexPath
method to determine whether or not a cell was selected.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("showQuestionnaire", sender: indexPath);
}
Then in your prepareForSegue
method
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "showQuestionnaire") {
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! QuestionnaireController
let row = (sender as! NSIndexPath).row; //we know that sender is an NSIndexPath here.
let patientQuestionnaire = patientQuestionnaires[row] as! PatientQuestionnaire
controller.selectedQuestionnaire = patientQuestionnaire
}
}
To explain...
performSegueWithIdentifier
within the prepare for segue method, because performSegueWithIdentifier
leads to prepareForSegue
; You are just looping around and around with no aim. (When you want to perform a segue, prepareForSegue is always executed)prepareForSegue
doesn't run by itself when a row is selected. This is where you need didSelectRowAtIndexPath
. You need a performSegueWithIdentifier
outside of the method as described previously, which should be in didSelectRowAtIndexPath