Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

ios - How to pass data from a viewcontroller to another tableview?

I am trying to clone iOS Note App. I am having an issue with passing data back from Viewcontroller to another Tableviewcontroller. (here from EditorViewController to MainViewController)

I have a Note class with Text and Date properties.

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.

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. I am really stuck at it, how should I write saveNote function to do that? I appreciate your help.

question from:https://stackoverflow.com/questions/65932109/how-to-pass-data-from-a-viewcontroller-to-another-tableview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As i said in my comments you can create a protocol to pass the array of Note to your MainViewController

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

Then your MainViewController needs to implement this protocol

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...