无法传递数据

时间:2018-06-29 21:13:38

标签: ios swift

我无法通过prepareForSegue将数据从MainVC传递到EditVC。问题的要点是我正在尝试编辑已经输入(保存在Firestore数据库中)的报价信息(报价和作者)。滑动(UIContextualAction)并点击“编辑”时,假设将数据传递到EditVC,然后将引号文本和作者文本放在可编辑的自己的UITextView中。编辑文字后,点击保存,它将更新Firestore中的条目;然后MainVC重新加载以更新其视图。 EditVC的segue可以正常工作,但是不会在各自的文本视图中显示引号和作者文本。我已经三天把头撞在墙上,试图弄清楚。非常感谢您的任何帮助或指导。我可以根据要求提供Github链接。

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(目标)

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
    }
}

1 个答案:

答案 0 :(得分:1)

在prepareForSegue函数中,您正在将目的地分配给发件人。应该是:

if let destination = segue.destination as? EditQuoteVC