Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 为什么不调用委托方法?_Swift_Swift Protocols - Fatal编程技术网

Swift 为什么不调用委托方法?

Swift 为什么不调用委托方法?,swift,swift-protocols,Swift,Swift Protocols,我正试图通知ChatViewController已使用协议删除MessagesViewController中的聊天,但从未调用ChatViewController中实现的委托方法。 在navigationController层次结构中,ChatViewController位于MessagesViewController的顶部 protocol MessagesViewControllerDelegate:class { func chatWasDeletedFromDatabase(chat

我正试图通知ChatViewController已使用协议删除MessagesViewController中的聊天,但从未调用ChatViewController中实现的委托方法。
在navigationController层次结构中,ChatViewController位于MessagesViewController的顶部

protocol MessagesViewControllerDelegate:class {
  func chatWasDeletedFromDatabase(chatUID: String)
}

class MessagesViewController: UITableViewController {
    weak var delegate: MessagesViewControllerDelegate?

  func observeChatRemoved() {
    print("it is gonna be called")
           //inform ChatViewController that a chat was deleted.
   self.delegate?.chatWasDeletedFromDatabase(chatUID: chat.chatUID)
    print("was called here") //prints as expected
  }
}


  class ChatViewController: JSQMessagesViewController {

     var messagesVC: MessagesViewController? 

    override func viewDidLoad() {
      super.viewDidLoad()

     messagesVC = storyboard?.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
     messagesVC?.delegate = self
   }
 }


    extension ChatViewController: MessagesViewControllerDelegate {
    func chatWasDeletedFromDatabase(chatUID: String) {
    print("chatWasDeletedFromDatabase called") //never prints out
      if self.chatSelected.chatUID == chatUID {
         //popToRootViewController
       }

 }
看来

weak var delegate: MessagesViewControllerDelegate?
如果
nil
您必须将其设置为
ChatViewController
呈现实例,无论您以何种方式呈现它

let chat = ///
self.delegate = chat
self.navigationController?.pushViewController(chat,animated:true)
也做

chat.messagesVC = self
就这样

 messagesVC = storyboard?.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
 messagesVC?.delegate = self

不是当前显示的messagesVC,因此注释上述两行已由
messagesVC
显示。我不明白你的解释。ChatVC定义了一个协议,MessagesVC采用该协议并将自己设置为委托。在
viewdiload
中,使用
messagesVC=storyboard?。将eviewcontroller(带有标识符:“MessagesViewController”)实例化为!MessagesViewController messagesVC?.delegate=self
。委托人MessagesVC应该将数据发送给委托人,即ChatVC。请向比我更有经验的人提问。您喜欢将代理设置为可选还是强制展开。今天我回答了一个问题,解释说我更喜欢使用
var委托:MessagesViewControllerDelegate!=零
正因为如此。任何类型的运行时测试都会立即暴露问题。我相信你的选择有好处。它们是什么?@bibscy您想从messagesVc向chatVc发送信息,因此您在messageVc中使用delegate var,它应该具有对chatVc的非零引用,而且您评论的两行代码将创建messageVc的另一个实例,而不是您导航到聊天室的实例,,,,,,因此,当您推送chatVc时,您应该存储对它的引用,这将使您能够调用它所调用的方法adopts@dfd可选,因为它不会使应用程序崩溃。@Sh_Khan我现在明白了。我感到困惑的原因是,到目前为止,我只使用协议将数据从自定义tableView单元格发送到tableViewController,而且我从未在tableView单元格类
self.delegate=tableView的实例中进行设置