[英]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.