我已经解决了我的问题,但我不知道是否有更好的方法。我正在制作锻炼日志。从 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。