在两个ViewController之间传递数据时出错

时间:2017-06-15 20:03:43

标签: ios swift tableview segue

将数据从一个ViewController传递到另一个ViewController后,出现以下错误。

no data
eventStruct(title: What are we gonna do today?, date: 2017-07-13, location: House3, description: Hello beautiful people!)
2017-06-15 22:01:05.697 RUC-App 1.0[48760:5792168] Warning: Attempt to present <RUC_App_1_0.EventViewController: 0x7fb686636460> on <RUC_App_1_0.NewsfeedViewController: 0x7fb68662e6a0> whose view is not in the window hierarchy!

第一个ViewController的代码:

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase

class NewsfeedViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var ref:DatabaseReference!,
        posts = [eventStruct]()
    @IBOutlet weak var tableview: UITableView!
    var propertStruct : (Any)? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        loadNews()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func loadNews() {
        ref = Database.database().reference()
        ref.child("events").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in

            if let valueDictionary = snapshot.value as? [AnyHashable:String]
            {
                let title = valueDictionary["Title"]
                let location = valueDictionary["Location"]
                let date = valueDictionary["Date"]
                let description = valueDictionary["Description"]
                self.posts.insert(eventStruct(title: title, date: date, location: location, description: description), at: 0)
                self.tableview.reloadData()
            }
        })
    }

    /////////////////////////     Table View Content     \\\\\\\\\\\\\\\\\\\\\\\\\\
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return posts.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let label1 = cell.viewWithTag(1) as! UILabel
        label1.text = posts[indexPath.row].title
        let label2 = cell.viewWithTag(2) as! UILabel
        label2.text = posts[indexPath.row].location
        let label3 = cell.viewWithTag(3) as! UILabel
        label3.text = posts[indexPath.row].date
        return cell
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "showDetails"){
            let secondViewController = segue.destination as? EventViewController
            secondViewController?.data = sender as? eventStruct
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "showDetails", sender: posts[indexPath.row])
    }
    /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
}

struct eventStruct {
    let title: String!
    let date: String!
    let location: String!
    let description: String!

}

第二个ViewController的代码:

import UIKit

class EventViewController: UIViewController {

    var data: eventStruct?
    @IBOutlet weak var titleLabel: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()
        print (data ?? "no data")
        self.titleLabel.text = self.data?.title

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

1 个答案:

答案 0 :(得分:2)

你不应该同时发生触发和手动分段。

试试这个:

删除您的didSelectRowAt功能(或将其注释掉),并将prepare()替换为:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "showDetails"){
        let secondViewController = segue.destination as? EventViewController
        if let idx = tableView.indexPathForSelectedRow {
            secondViewController?.data = posts[idx.row]
        }
    }
}