在这里,我尝试使用协议将值从一个类LanguageSelectionTVC
传递到另一个RegistrationVC
。
当我尝试在self.delegate?.setSelectedLangauges(self.languagesSpokenArray)
内调用方法LanguageSelectionTVC
时,它不会调用类setSelectedLangauges
中的方法Registration VC
,有人可以建议我在哪里出错了?
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)
}
}
从LanguageSelectionTVC
移至RegistrationVC
。以下tableView
方法位于我的RegistrationVC
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)
}
}
答案 0 :(得分:2)
您需要从preparForSegue
访问delegate
以设置override
。因此prepare(for:sender:)
中RegistrationVC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "LanguageSelectionTVC" {
let vc = segue.destination as! LanguageSelectionTVC
vc.delegate = self
}
}
。{/ p>
googleapis
答案 1 :(得分:1)
您需要将RegistrationVC中的委托设置为LanguageSelectionTVC,例如您可以在执行segue时设置它:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let targetVC = segue.destination as! LanguageSelectionTVC
targetVC.delegate = self
}
答案 2 :(得分:0)
您需要设置委托。你能不能看到下面两个视图控制器之间传递数据的准备例子。
import UIKit
protocol SenderViewControllerDelegate
{
func messageData(str1:NSString)
}
class tableViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource
{
var delegate: tableViewController?
override func viewDidLoad()
{
super.viewDidLoad()
self.delegate?.messageData(str1: “Hello world”)
}
}
接收以下控制器中的数据
import UIKit
class HomeViewController: UIViewController ,SenderViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let objtableview = tableViewController(nibName: "tableViewController",bundle: nil)
objtableview.delegate = self
self.navigationController?.pushViewController(objtableview, animated: true)
}
func messageData(str1:NSString){
print(str1)
}
}