我有一个 ViewController (planExercise),它存储一组练习对象,
class planExercise: UIViewController {
var exerciseList = [
Exercise(type: "Cardiovascular", name: "Walking", imageName: "walking", calories: 150, time: 30, isComplete: false),
Exercise(type:"Strength",name:"Chair Squats",imageName:"cycling", calories: 200, time: 15, isComplete: false),
Exercise(type: "Flexibility", name: "Seated Toe Touch", imageName: "squats", calories: 100, time: 15, isComplete: false)
]
我有另一个 ViewController (TableViewDetail),当我单击按钮时,它基本上会添加到exerciseList 数组中,这就是我到目前为止所拥有的:
@IBAction func didTapAdd(){
planExercise().exerciseList.append(selectedExercise)
}
我尝试在TableViewDetail ViewController中调用planExercise ViewController,然后访问其中的exerciseList数组并将selectedExercise附加到其中,但是当我运行应用程序时,planExercise ViewController中的exerciseList数组没有任何反应,并且它没有更新.
因为
planExercise()
这里
planExercise().exerciseList.append(selectedExercise)
是一个新实例,而不是显示的实例,您需要使用委托来传递您想要更改其数组的实际实例
class TableViewDetail: UIViewController {
weak var delegate:YourDelegate?
.....
@IBAction func didTapAdd(){
delegate?.exerciseList.append(selectedExercise)
}
}
现在当你从里面展示
TableViewDetail
时planExercise
let des = TableViewDetail()
des.delegate = self
present(des.....