简体   繁体   English

如何在容器视图和主视图控制器之间正确传递数据

[英]How to pass data between container view and main view controller properly

I'm dealing with following problem: On main VC I have a TableView, and on container view controller I have a textField. 我正在处理以下问题:在主VC上,我有一个TableView,在容器视图控制器上,我有一个textField。 I want to add every text, I'm typing in container automatically appears as a new row on tableView in main VC 我想添加所有文本,我在容器中输入内容会自动显示为主VC中tableView上的新行

By now I'm using segue to send data from main VC to container. 现在,我正在使用segue将数据从主VC发送到容器。 But what should I implement to do the same in a reverse order? 但是,我应该怎么做才能以相反的顺序执行相同的操作? I though of implementing delegate of main VC in my container view, but I have no idea how to do that properly. 虽然我在容器视图中实现了主要VC的委托,但是我不知道如何正确执行。 Or maybe there is exist more common solution. 或者,也许存在更常见的解决方案。

Anyway, here is my code: 无论如何,这是我的代码:

class MessageViewController: UIViewController {

    var currentUser: User!

    var containerViewController: InputTextViewController?

    @IBOutlet weak var enterMessageView: UIView!
    @IBOutlet weak var messageTableView: UITableView!
}

extension MessageViewController {

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "moveToInputText" {
            let connectContainerViewController = segue.destination as? InputTextViewController
            containerViewController = connectContainerViewController

            containerViewController?.userSendMessageTo = currentUser

        }
    }
}

extension MessageViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return currentUser.mesaageHistory.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell") as! ChatMessageTableViewCell
        let data = currentUser.mesaageHistory[indexPath.row]
        cell.messageLabel.text = data.messageText

        return cell
    }
}

class InputTextViewController: UIViewController {

    @IBOutlet weak var messageTextField: UITextField!

    var userSendMessageTo: User!

    weak var delegate = MessageViewController()

    @IBAction func sendMessge(_ sender: Any) {

        handleSend()
        userSendMessageTo.mesaageHistory.append(message)

        print(userSendMessageTo.mesaageHistory[0].messageText)

        let row = userSendMessageTo.mesaageHistory.count - 1
        let insertIndexPath = IndexPath(item: row, section: 0)

        print(userSendMessageTo.mesaageHistory.count)

        delegate?.messageTableView.beginUpdates()
        delegate?.messageTableView.insertRows(at: [insertIndexPath], with: UITableViewRowAnimation.automatic)
        delegate?.messageTableView.endUpdates()

    }

}

Here's how to use the 'delegate' pattern properly 这是正确使用“委托”模式的方法

  1. Protocol declaration & delegate member 协议声明和委托成员

     protocol InputTextViewControllerDelegate: class { func someFunc() func anotherFunc() } class InputTextViewController: UIViewController { weak var delegate: InputTextViewControllerDelegate? } 
  2. Protocol implementation & setting the delegate property 协议实现和设置委托属性

     extension MessageViewController, InputTextViewControllerDelegate { // MARK: InputTextViewControllerDelegate // func someFunc() { } func anotherFunc() { } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "moveToInputText" { let connectContainerViewController = segue.destination as? InputTextViewController containerViewController = connectContainerViewController containerViewController } } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从View控制器传递到Container? - How to pass data from View controller to Container? 在容器视图中将数据传递到控制器 - Pass data to controller in container view 如何在iOS Swift中使用回调在视图控制器和容器视图之间传递数据? - How to pass data between view controller and Container view using callbacks in iOS Swift? 在ios 6中,如何在View控制器中使用“容器视图”对象和嵌入在其中的表视图控制器之间使用传递数据? - In ios 6, how do you use pass data between View controller holding 'Container View' object and Table View controller that is embedded in it? 如何将数据传递到容器视图中的视图控制器? - How to pass data to a view controller within an container view? 如何将数据从View Controller传递到Container View? - How would I pass data from a View Controller to a Container View? 如何将数据从视图 Controller 传递到 swift 中的容器视图 - How to pass data from View Controller to Container View in swift 在视图控制器之间传递数据 - Pass data between view controller 将数据传递到嵌入在Container View Controller中的View Controller - Pass data to View Controller embedded inside a Container View Controller 如何将数据从父视图控制器传递到子容器视图控制器 - How to pass data from parent view controller to child container view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM