1

I have a UIViewController where i need to pass information.For that i have made another UIViewController where i have a CollectionView.

So when a user clicks on an element from the CollectionView it should close the view and go back to the main UIViewController.

So i have managed to get it working with a button in the Navigaiton bar.

But when i place the same code

"dismissViewControllerAnimated(true, completion: nil)"

in the didSelectItemAtIndexPath its not working.

I have tried to place a button in the cell but still same result everything works except that.

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.exercisesNames.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellexercises_insert", forIndexPath: indexPath) as! Insert_ExerciseCollectionViewCell

    cell.insert_exerciseImage?.image = self.exercisesImages[indexPath.row]
    cell.insert_exerciseLabel?.text = self.exercisesNames[indexPath.row]
    cell.insert_exercisesHardnessImg?.image = self.exercisesHardnessImg[indexPath.row]

    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
     exercise_label="\(exercisesNames[indexPath.row])"
     exercise_image="\(exercises_Exervise_Row_Names[indexPath.row])"
     //self.dismissViewControllerAnimated(true, completion: nil)

     closeMe()
}




@IBAction func done(sender: AnyObject) {
    closeMe()
}

func closeMe(){
dismissViewControllerAnimated(true, completion: nil)
}

2 Answers2

1

I am not sure because the context but I guess that you are trying to dismiss the collectionViewController but it wasn't presented modally so you can try:

self.navigationController?.popViewControllerAnimated(true)

instead of dismissViewControllerAnimated(true, completion: nil)

If in fact it was presented modally, make sure that your collectionViewController is embedded in a UINavigationController.

PabloLerma
  • 620
  • 5
  • 12
  • If i do that it dismisses the view but there is no effect on my unwindToMainViewController segue –  Oct 13 '15 at 17:44
0

If it's presented modally you need to implement an unwind segue function in your first view controller.

@IBAction func unwindToMainViewController(segue: UIStoryboardSegue) {
}

Once you have done so go to the storyboard and control-drag from your view controller icon on the second view controller to the exit icon of your main view controller which you will be able to see on the bar to the left. It will then let you select your unwind segue.

Select your segue and set a segue identifier for it.

For your didSelectItemAtIndexPath function, you must call your segue.

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("segueIdentifier", sender: self)
}

To send objects to your main view controller you can use the following function in your second view controller.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {       
    if(segue.identifier == "segueIdentifier") {
        let mainViewController = (segue.destinationViewController as mainViewController)
        mainViewController.classVariable = newVariableValue
    }
}

For more information please refer to:

What are Unwind segues for and how do you use them?

Community
  • 1
  • 1
Qasim Asghar
  • 590
  • 6
  • 12
  • I have done that my problem is that i need to dismiss the view in order to add the exercise to my workout.Its now working from the collectionView so i need to get that working –  Oct 13 '15 at 17:37