繁体   English   中英

协议/代表 - 发现代表为零

[英]Protocol / Delegate - Delegate found nil

几天来,我一直在绞尽脑汁想弄清楚这一点。 我有两个视图控制器(WorkoutViewController 和 WorkoutAlertViewController)。 WorkoutViewController 是创建的所有锻炼的 tableView。 它具有用于编辑/删除的滑动操作。 这个视图 controller 也有一个“+”作为条形按钮来打开 WorkoutAlertViewController。 此外,在滑动操作编辑时,WorkoutAlertViewController 会打开。

在此处输入图像描述

WorkoutAlertViewController 从用户那里获取输入,并在按下 Add 按钮时将数据传回 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:文件。 据我所知,没有创建锻炼委托(nil)。

与往常一样,非常感谢任何指导。

您不需要委托进行前向通信..您可以直接调用 function

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 做

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
        
    }
    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM