在这里,我试图通过使用协议将值从一个类传递到另一个类。
当我尝试调用LanguageSelectionTVC内部的方法RegistrationVC时,它不会调用self.delegate?.setSelectedLangauges(self.languagesSpokenArray)内部的方法LanguageSelectionTVC有人能告诉我哪里出错了吗?

protocol LanguageSelectionTVCProtocol {
    func setSelectedLangauges(_ valueSent: [String])
}


class LanguageSelectionTVC: UITableViewController {
    var delegate                    : LanguageSelectionTVCProtocol? = nil

   func saveAndClose() {
        self.delegate?.setSelectedLangauges(self.languagesSpokenArray)
        dismiss()
   }

}


class RegistrationVC:           UIViewController,
                                UITableViewDelegate,
                                UITableViewDataSource,
                                LanguageSelectionTVCProtocol{

    func setSelectedLangauges(_ valueSent: [String]){
         self.showLanguagesSpoken(valueSent)
    }
}

setSelectedLangauges移动到Registration VC。下面的LanguageSelectionTVC方法在myRegistrationVC
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let row     = indexPath.row
        let section = indexPath.section

        let currentCell = tableView.cellForRow(at: indexPath) as! UITableViewCell
        if section == 4 && row == 0 {
            // The user has clicked on languages spoken cell
            self.performSegue(withIdentifier: "LanguageSelectionTVC", sender: self)
        }
    }

最佳答案

您需要从LanguageSelectionTVC访问preparForSegue来设置它的delegate。所以你的override里的prepare(for:sender:)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "LanguageSelectionTVC" {
        let vc = segue.destination as! LanguageSelectionTVC
        vc.delegate = self
    }
}

10-07 12:05