简体   繁体   中英

Delegate function not being called iOS Swift

Here I am trying to pass value from one class LanguageSelectionTVC to another RegistrationVC by using a Protocol.

When I try and call the method self.delegate?.setSelectedLangauges(self.languagesSpokenArray) inside LanguageSelectionTVC it doesn't call the method setSelectedLangauges inside the class Registration VC could someone please suggest where I am going wrong ?

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

Moving to LanguageSelectionTVC from RegistrationVC . The below tableView method is in my 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)
        }
    }

You need to access LanguageSelectionTVC from preparForSegue to set delegate of it. So override the prepare(for:sender:) in your RegistrationVC .

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

You need to set the delegate in RegistrationVC to LanguageSelectionTVC, for example you could set it when performing a segue:

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

You need to set delegate. can you please see below ready example for pass data between two view controller.

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

receive data in below controller

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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