Joe Vargas
Joe Vargas

Reputation: 65

Trouble passing data

I’m having trouble passing data via prepareForSegue from MainVC to EditVC. The gist of the problem is I’m trying to edit quote info(quote and author) already entered(saved in a Firestore database). When swiping(UIContextualAction) and tap on edit, it’s suppose to pass the data on to the EditVC where it’ll put the quote text and author text in their own UITextView where it’s editable. Once you edit the text, hit save and it’ll update the entry in Firestore; then MainVC reloads to update it’s view. The segue to the EditVC works flawlessly but it doesn’t display the quote & author text in their respective text views. I’ve been banging my head against the wall for 3 days trying to figure it out. Any help or guidance from you all is greatly appreciated. I can provide the Github link upon request.

MainVC:

    }
        let edit = UIContextualAction(style: .normal, title: "Edit") { (action, view, nil) in
            self.performSegue(withIdentifier: "toEditQuote", sender: self.quotes)
            print("Segue initiated")
        }
        edit.backgroundColor = #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)
        return UISwipeActionsConfiguration(actions: [delete, edit])
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?){
        print("preparing to segue")
        if let destination = segue.destination as? EditQuoteVC {
            print("Destination = EditQuoteVC")
            if let quoteData = sender as? Quote {
                print("data passing")
                destination.quoteTxt = quoteData.quoteTxt
                destination.authorTxt = quoteData.authorTxt
                print("data passed")
            }
        }
    }
}

EditQuoteVC(destination)

class EditQuoteVC: UIViewController {

//Outlets
@IBOutlet weak var quoteText: UITextView!
@IBOutlet weak var authorText: UITextView!

//Variables
var quoteTxt: String!
var authorTxt: String!

override func viewDidLoad() {
super.viewDidLoad()
quoteText.text = quoteTxt
authorText.text = authorTxt
}

Quote.swift

class Quote {

    private(set) var quoteTxt: String!
    private(set) var authorTxt: String!
    private(set) var timestamp: Date!
    private(set) var userId: String!
    private(set) var documentID: String!


    init(quote: String, author: String, timestamp: Date, userId: String, documentID: String) {

        self.quoteTxt = quote
        self.authorTxt = "- \(author)"
        self.timestamp = timestamp
        self.userId = userId
        self.documentID = documentID
    }
    class func parseData(snapshot: QuerySnapshot?) -> [Quote] {
        var quotes = [Quote]()
        guard let snap = snapshot else { return quotes}
        for document in snap.documents { //Grabs the documents...
            let data = document.data()
            let quote = data[QUOTE_TEXT] as? String ?? ""
            let author = data[AUTHOR_TEXT] as? String ?? ""
            let timestamp = data[TIMESTAMP] as? Date ?? Date()
            let userId = data[USER_ID] as? String ?? ""
            let documentID = document.documentID

            let newQuote = Quote(quote: quote, author: author, timestamp: timestamp, userId: userId, documentID: documentID)
            quotes.append(newQuote)
        }
        return quotes
    }
}

Upvotes: 0

Views: 82

Answers (1)

GntlmnBndt
GntlmnBndt

Reputation: 259

In your prepareForSegue function, you are assigning destination to sender. It should be:

if let destination = segue.destination as? EditQuoteVC

Upvotes: 1

Related Questions