找到你要的答案

Q:Call view controller method from custom class

Q:从自定义类调用视图控制器方法

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 ?

我有一个自定义类和一个视图控制器。

我的自定义类:

class ChatManager:NSObject {

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

当消息到达时,我从互联网上需要更新tableview视图控制器。所以我的意思是,我要叫messagearrived方法的视图控制器的方法。我该怎么做?

answer1: 回答1:

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

下面是一个使用委托的简单例子:

在聊天管理器类前声明委托

protocol ChatManagerDelegate {
    func manageMessage()
}

消息到达时,调用委托方法处理它。

class ChatManager: NSObject {

    var delegate: ChatManagerDelegate?

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

在视图控制器中,请记住将聊天管理器的委托设置为自。

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

    func manageMessage() {
        self.updateTable()
    }
}
answer2: 回答2:

This would be a possible implementation:

ViewController:

class ViewController: UIViewController,ChatManagerDelegate {

  override func viewDidLoad() {
    super.viewDidLoad()
    let myChatManager = ChatManager()
      myChatManager.delegate = self
  }


  func messageDidArrive() {
    // Do Things here.
  }
}

Chatmanager:

class ChatManager:NSObject {
  var delegate:ChatManagerDelegate?

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

Delegate-Protocol:

protocol ChatManagerDelegate{
  func messageDidArrive()
}

这将是一个可能的实现:

ViewController:

class ViewController: UIViewController,ChatManagerDelegate {

  override func viewDidLoad() {
    super.viewDidLoad()
    let myChatManager = ChatManager()
      myChatManager.delegate = self
  }


  func messageDidArrive() {
    // Do Things here.
  }
}

Chatmanager:

class ChatManager:NSObject {
  var delegate:ChatManagerDelegate?

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

委托协议:

protocol ChatManagerDelegate{
  func messageDidArrive()
}
ios  swift