MSMessagesViewController delegate returns nil for active conversation

Viewed 75

I am trying to pass down the activeConversation inside the initial view controller to another view controller. The way I am doing this is by setting an variable called currentConversation to the activeConversation. However whenever I try to access that attribute it returns nil.

Initial View Controller:

    vc.displayType = self.passSelectedPhrase
    vc.currentConversation = activeConversation
    performSegue(withIdentifier: "showDetail", sender: selectedPhrase)

Messages View Controller (delegate):

var currentConversation : MSConversation!
func saySomething(value: String){

    if ((currentConversation) != nil) {
        currentConversation?.insertText("hello there", completionHandler: nil)
        currentConversation?.insertText(value, completionHandler: nil)
    }else{
        print("No active conversation")
    }
}

Returns "No active conversation"

1 Answers

It looks like you are manually initiating a view controller, setting the variables, then calling a segue.

The segue will initiate its own view controller, so the one you initiate isn’t actually used.

You need to use the prepareForSegue method to set your variables as this has a reference to the view controller that is about to be displayed.

Related