0

I have solved my problem, but I don't know if there is some better way of doing that.I am making a workout log. From VC1 I want to select a date and push the VC 2 to select an exercise and then return the exercise to VC1. I am getting the returned data using delegation like that.

VC1

//Here the date of the new Workout. I have to get the exercise first.
func addButtonInSectionDidClick(date:NSDate) {

   //Delegate will call exerciseSelectionDismissedWithExerciseName        
   exerciseSelectionWireframe!.dismissDelegate = self 

   //Push the VC 2 for exercise selection
   exerciseSelectionWireframe!.presentExerciseSelection()                   

 }

func  exerciseSelectionDismissedWithExerciseName(name:String) {

        //Returned but I can't use the var date anymore.


    }

There is the problem. I can not use the var date, in the delegation method, but I need to.

My workaround is using a completion block as private var.

    private var completionBlock: (() -> Void)?

    //Here the date of the new Workout. I have to get the exercise first.
    func addButtonInSectionDidClick(date:NSDate) {

       //Delegate will call exerciseSelectionDismissedWithExerciseName        
       exerciseSelectionWireframe!.dismissDelegate = self 

       //Push the VC 2 for exercise selection
       exerciseSelectionWireframe!.presentExerciseSelection() 

       completionBlock = {        
          var myDate = date   
      }               

 }

    func  exerciseSelectionDismissedWithExerciseName(name:String) {

        if let completionBlock = completionBlock {
                     completionBlock()     
        }

    }

Is there some other predefined way of doing this? I don't like having that completionBlock var there.

crom87
  • 1,141
  • 9
  • 18

1 Answers1

0

Use a share preference or use a singleton, something like:

import Foundation
class ConstantData {
var myDate:NSDate    = NSDate()
}

let sharedDateAccess = ConstantData()

So you can change the variable value and you can access it from anywhere:

func addButtonInSectionDidClick(date:NSDate) {

   //Delegate will call exerciseSelectionDismissedWithExerciseName        
   exerciseSelectionWireframe!.dismissDelegate = self 

   //Push the VC 2 for exercise selection
   exerciseSelectionWireframe!.presentExerciseSelection() 

   sharedDataAccess.myDate = date   

 }
Hernan Duque
  • 73
  • 1
  • 5