0

我已经解决了我的问题,但我不知道是否有更好的方法。我正在制作锻炼日志。从 VC1 我想选择一个日期并按下 VC 2 选择一个练习,然后将练习返回给 VC1。我正在使用这样的委托获取返回的数据。

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.


    }

有问题。我不能在委托方法中使用 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()     
        }

    }

还有其他一些预定义的方法吗?我不喜欢那里有completionBlock var。

4

1 回答 1

0

使用共享偏好或使用单例,例如:

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

let sharedDateAccess = ConstantData()

因此,您可以更改变量值,并且可以从任何地方访问它:

func addButtonInSectionDidClick(日期:NSDate){

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

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

   sharedDataAccess.myDate = date   

 }
于 2015-08-01T12:09:22.400 回答