几天来我一直在努力解决这个问题。我有两个视图控制器(WorkoutViewController和WorkoutAlertViewController)。 WorkoutViewController是用于创建的所有锻炼的tableView。它具有用于编辑/删除的滑动动作。该视图控制器还带有一个“ +”作为条形按钮,以打开WorkoutAlertViewController。另外,在滑动动作编辑中,WorkoutAlertViewController也将打开。
按下“添加”按钮后,WorkoutAlertViewController会从用户那里获取输入,并将数据传递回WorkoutViewController。
这是问题所在。我有一个协议/委托,它将用户的输入输入到WorkoutAlertViewController中,并将信息传递回WorkoutViewController以填充tableView。这很棒!但是,当用户想要编辑锻炼时,我需要该锻炼的信息来填充WorkoutAlertViewController上的条目。无论我尝试了什么,此协议的委托始终返回nil。因此,不会传递任何数据。
这是WorkoutViewController的必要代码:
protocol PassExistingWorkout {
func didTapEdit(workout: Workout)
}
class WorkoutViewController: UIViewController {
var workoutDelegate: PassExistingWorkout!
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
let alertVC = storyboard!.instantiateViewController(withIdentifier: "WorkoutVC") as! WorkoutAlertViewController
alertVC.alertDelegate = self
present(alertVC, animated: true, completion: nil)
}
}
extension WorkoutViewController: PassNewWorkout {
func didTapAdd(name: String, time: String, date: Date) {
workoutName = name
averageTime = time
creationDate = date
let newWorkout = Workout()
newWorkout.name = workoutName
newWorkout.dateCreated = creationDate
newWorkout.time = averageTime
saveWorkout(workout: newWorkout)
}
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, view, actionPerformed) in
if let _ = tableView.cellForRow(at: indexPath){
do {
try self.realm.write {
self.realm.delete(self.workoutItems![indexPath.row])
}
} catch {
print("Error saving category \(error)")
}
}
tableView.reloadData()
}
deleteAction.backgroundColor = .red
let editAction = UIContextualAction(style: .normal, title: "Edit") { (action, view, actionPerformed) in
let alertVC = self.storyboard!.instantiateViewController(identifier: "WorkoutVC") as! WorkoutAlertViewController
self.present(alertVC, animated: true, completion: nil)
if let _ = tableView.cellForRow(at: indexPath){
self.passedWorkout = self.workoutItems![indexPath.row]
self.workoutDelegate.didTapEdit(workout: self.passedWorkout)
}
}
editAction.backgroundColor = .gray
return UISwipeActionsConfiguration(actions: [deleteAction, editAction])
}
}
这是WorkoutAlertViewController的代码:
protocol PassNewWorkout {
func didTapAdd(name: String, time: String, date: Date)
}
class WorkoutAlertViewController: UIViewController {
var alertDelegate: PassNewWorkout!
@IBAction func addButtonPressed(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
workoutName = workoutTextField.text!
creationDate = datePicker.date
averageTime = timeTextField.text!
alertDelegate.didTapAdd(name: workoutName, time: averageTime, date: creationDate)
}
override func viewDidLoad() {
super.viewDidLoad()
let workoutVC = storyboard!.instantiateViewController(identifier: "Main") as! WorkoutViewController
workoutVC.workoutDelegate = self
}
extension WorkoutAlertViewController: PassExistingWorkout {
func didTapEdit(workout: Workout) {
receivedWorkout = workout
}
}
应用程序在调用时在editAction中崩溃:
self.workoutDelegate.didTapEdit(workout: self.passedWorkout)
错误:致命错误:意外地发现nil,同时隐式展开了一个可选的value:文件。据我所知,没有创建ExerciseDelegate(无)。
一如既往,任何指导都将不胜感激。
答案 0 :(得分:-1)
您不需要委托进行正向通信..您可以直接直接调用该函数
let editAction = UIContextualAction(style: .normal, title: "Edit") { (action, view, actionPerformed) in
let alertVC = self.storyboard!.instantiateViewController(identifier: "WorkoutVC") as! WorkoutAlertViewController
self.passedWorkout = self.workoutItems![indexPath.row]
alertVC.didTapEdit(workout: self.passedWorkout)
self.present(alertVC, animated: true, completion: nil)
}
在你其他班上做
class WorkoutAlertViewController: UIViewController {
var alertDelegate: PassNewWorkout!
@IBAction func addButtonPressed(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
workoutName = workoutTextField.text!
creationDate = datePicker.date
averageTime = timeTextField.text!
alertDelegate.didTapAdd(name: workoutName, time: averageTime, date: creationDate)
}
override func viewDidLoad() {
super.viewDidLoad()
}
func didTapEdit(workout: Workout) {
receivedWorkout = workout
}
}