Protocol Method Is Not Calling

Tolgay Toklar

I am trying to use protocols with swift. I don't know why it is not working.

Second View controller:

protocol passBackTheVoice {
    func sendVoiceMessage(name: String)
}

class VoiceRecordViewController: UIViewController {
    var passBackDelegate: passBackTheVoice?
    ….
    ….

    func uploadCompleted() {
            self.passBackDelegate?.sendVoiceMessage(voiceName)
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

First View Controller:

class ChatViewController: UIViewController, passBackTheVoice {
    var voiceRecordVC = VoiceRecordViewController()

    override func viewDidLoad() {
        voiceRecordVC.passBackDelegate=self
    }

    func sendVoiceMessage(name: String) {
        print("chat view \(name)")
    }
}

My sendVoiceMessage method is not calling. Where is the problem and how can i solve?

Ps: Don't know this is related but, firstly my ChatViewController is showing then user touches to a button i am showing VoiceRecordViewController to user. Then user creating a voice file and i want to pass back this file to my ChatViewController.

Mücahit Tekin

Firstly if you present your view controller from segue you have to use this code:

class ChatViewController: UIViewController, passBackTheVoice {


        override func viewDidLoad() {

        }

        func sendVoiceMessage(name: String) {
            print("chat view \(name)")
        }

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

            if segue.identifier == "YOUR_IDENTIFIER_IN_STORYBOARD" {
                let vc = segue.destination as! VoiceRecordViewController
                vc.delegate = self
            }

        }


    }

If you present manually use this code:

class ChatViewController: UIViewController, passBackTheVoice {

        override func viewDidLoad() {


        }

        func sendVoiceMessage(name: String) {
            print("chat view \(name)")
        }

        func goTovoiceRecordVC(){
            let voiceRecordVC = VoiceRecordViewController() // load from storyboard or nib whatever you want
            voiceRecordVC.passBackDelegate=self
            present(voiceRecordVC, animated: true, completion: nil)
        }

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related