简体   繁体   English

如何将数据从视图控制器传递到另一个表视图?

[英]How to pass data from a viewcontroller to another tableview?

I am trying to clone iOS Note App.我正在尝试克隆 iOS Note App。 I am having an issue with passing data back from Viewcontroller to another Tableviewcontroller.我在将数据从 Viewcontroller 传回另一个 Tableviewcontroller 时遇到问题。 (here from EditorViewController to MainViewController) (这里从 EditorViewController 到 MainViewController)

I have a Note class with Text and Date properties.我有一个带有文本和日期属性的注释 class。

class Note: Codable {
    var text: String
    var modificationDate: Date
    
    init(text: String, modificationDate: Date) {
        self.text = text
        self.modificationDate = modificationDate
    }
}

TableView:表视图:

class MainViewController: UITableViewController {
    
    var notes = [Note]()

    @objc func createNoteTapped(noteIndex: Int) {
        if let vc = storyboard?.instantiateViewController(identifier: "EditorViewController") as? EditorViewController {
            navigationController?.pushViewController(vc, animated: true)
        }
    } 
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return notes.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...  
    }
}
    

and ViewController that should send Note data back to obove tableview.和 ViewController 应该将 Note 数据发送回 obove tableview。

class EditorViewController: UIViewController {
    
    var notes: [Note]!
    var noteIndex: Int!

    @IBOutlet var textView: UITextView!
    
    func setParameters(notes: [Note], noteIndex: Int) {
        self.notes = notes
        self.noteIndex = noteIndex
        notes.append(notes[noteIndex])
    }
    
    @objc func saveNote() {

     ...

    }

}

In the EditorViewcontroller I have a textview, I want to save text inside of it as a Note text, and load that Note into TableView when I tap saveNote.在 EditorViewcontroller 中,我有一个 textview,我想将其中的文本保存为注释文本,并在我点击 saveNote 时将该注释加载到 TableView 中。 I am really stuck at it, how should I write saveNote function to do that?我真的卡住了,我应该怎么写 saveNote function 来做到这一点? I appreciate your help.我感谢您的帮助。

Instead of using the delegate pattern , you can use closures to pass data back and forward.除了使用delegate pattern ,您还可以使用closures来回传递数据。

in your EditorViewController create the call back closure as like在您的 EditorViewController 中创建回调闭包

class EditorViewController: UIViewController {

        var onCompletion: ((notes: [Note]) -> ())?
        var notes: [Note]!
        var noteIndex: Int!

on your save note action set the saved data in the completion part as like在您的保存笔记操作中,将完成部分中的保存数据设置为

@IBAction func saveNote() {
    onCompletion?(notes: notes)
            self.navigationController?.popViewController(animated: true)

}

finally process your data on MainViewController like as最后像这样在 MainViewController 上处理你的数据

   @objc func createNoteTapped(noteIndex: Int) {
    if let vc = storyboard?.instantiateViewController(identifier: "EditorViewController") as? EditorViewController {
         vc.onCompletion = { notes in
        // this will be executed when `saveNote(_:)` will be called
        print(notes)
        // refresh your tableview
    }
        navigationController?.pushViewController(vc, animated: true)
    }
} 

As i said in my comments you can create a protocol to pass the array of Note to your MainViewController正如我在评论中所说,您可以创建一个protocol来将Note数组传递给您的MainViewController

protocol YourProtocolName {
    func updatedNotes(notes: [Note])
}

Then your MainViewController needs to implement this protocol然后你的MainViewController需要实现这个协议

extension MainViewController: YourProtocolName {
   func updatedNotes(notes: [Note]) {
      //Do whatever you need to do here
   }
}

After this you need to declare MainViewController as EditorViewController delegate adding weak var delegate: YourProtocolName in your EditorViewController class, and lastly you need to modify createNoteTapped function in MainViewController passing self as EditorViewController delegate在此之后,您需要将MainViewController声明为EditorViewController委托,添加weak var delegate: YourProtocolName在您的EditorViewController class 中,最后您需要修改MainViewController中的createNoteTapped function,将 self 作为EditorViewController委托

@objc func createNoteTapped(noteIndex: Int) {
        if let vc = storyboard?.instantiateViewController(identifier: "EditorViewController") as? EditorViewController {
            vc.delegate = self
            navigationController?.pushViewController(vc, animated: true)
        }
    } 

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

相关问题 如何用另一个ViewController的数据填充TableView - How to fill TableView with data from another ViewController 在Swift中的TableView和另一个ViewController之间传递数据 - Pass data between TableView and another ViewController in Swift 我如何将数据从SlideTableTableViewController传递到ViewController中嵌入的TableView - How i pass Data from SlideOut TableViewController to TableView embedded in ViewController 如何使用Swift 3将数据从ViewController A传递到另一个ViewController B - How to pass data from viewcontroller A to another viewcontroller B using swift 3 将数组数据从ViewController传递到TableView - Pass arrays data from a ViewController to a TableView Swift-将数据从TableView传递到第三个ViewController - Swift - pass data from tableview to third viewcontroller 将数据从tableview传递回Viewcontroller - Pass data back to Viewcontroller from tableview 如何将MVC值从TableView传递到ViewController - How to pass MVC values from TableView to ViewController 斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel? - Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview? 如果tableView数据为空,如何显示另一个ViewController - How to show another viewcontroller if tableView data is empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM