0

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.

Tolgay Toklar
  • 4,151
  • 8
  • 43
  • 73
  • 3
    `VoiceRecordViewController()` creates a brand new instance of the controller. It's **not** the controller instance in the storyboard. – vadian Jan 28 '17 at 22:04
  • Did you attempt some debugging? Does `uploadCompleted()` ever get called? If so, is `passBackDelegate` `nil` inside it? – Hamish Jan 28 '17 at 22:05
  • I debugged and i am sure uploadCompleted is calling. But when i check this line : self.passBackDelegate?.sendVoiceMessage(voiceName) passBackDelegate is looking nil. I guess this is the problem. How to resolve? – Tolgay Toklar Jan 28 '17 at 22:06
  • it is Swift convention to name your protocols starting with an uppercase letter – Leo Dabus Jan 28 '17 at 22:18
  • See vadian's point. You are setting the `delegate` of `VoiceRecordViewController` that you instantiate here, rather than the one that is instantiated when you segue to it. Instead, have a `prepareForSegue` that sets it. See http://stackoverflow.com/a/9736559/1271826. – Rob Jan 28 '17 at 22:19
  • What happens if i use sharedInstance instead of creating a new instance? – Tolgay Toklar Jan 28 '17 at 22:24
  • how do you present `VoiceRecordViewController`? from code? or via segue from storyboard? – André Slotta Jan 28 '17 at 22:28
  • Then set `passBackDelegate` of that `sharedInstance`. (You obviously lose the ability to use storyboard segues at that point.) But you're obviously not transitioning to the `voiceRecordVC` that you instantiated here because your `passBackDelegate` is `nil`. By the way, if you're really using a shared instance of that `VoiceRecordViewController`, then you should (a) make your protocol a `class` protocol; and (b) define `passBackDelegate` to be `weak`. You can only get away with sloppiness of not using weak delegates if you know that the presented VC will be released before the parent VC. – Rob Jan 28 '17 at 22:31

1 Answers1

1

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

    }
Mücahit Tekin
  • 256
  • 3
  • 11