Call view controller method from custom class

I have a custom class and a view controller.

My custom class:

class ChatManager:NSObject {

    func messageArrived() {
         //When Message arrives I am handling it from here
         //I need something like that: Viewcontroller.updateTable()
    }
}

When message arrives from internet I need to update tableview in view controller. So I mean I have to call a view controller method from messageArrived method. How can I do this ?

Answers


Here is a simple example of using delegate:

declare the delegate before your chat manager class

protocol ChatManagerDelegate {
    func manageMessage()
}

when the message arrived, call the delegate method to handle it.

class ChatManager: NSObject {

    var delegate: ChatManagerDelegate?

    func messageArrived() {
        self.delegate!.manageMessage()
    }
}

in your view controller, remember to set the delegate of the chat manager to self.

class ViewController: ChatManagerDelegate {
    var manager = ChatManager()
    manager.delegate = self

    func manageMessage() {
        self.updateTable()
    }
}

Need Your Help

How to use superscript HTML code with radio button label with AUI

html radio-button superscript liferay-aui

How to use superscript external HTML code with radio button label along with AUI

Is there any reason the C# / .NET compiler(s) do not warn about Dispose()?

c# .net vb.net idisposable

I was thinking about this just today whilst I was writing some IDisposable code.