在这里,我试图通过使用协议将值从一个类传递到另一个类。
当我尝试调用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
}
}